首页 > 解决方案 > 如何显示数据库中的每一行数据?

问题描述

我很难在数据库的每一行上显示标题和注释。我想在从页面标题中的表单到显示行数据页面的每一行之后显示一行(带有标题和注释)。

这是我下面的代码:

// 假设我们有 4 行数据。在我的代码中,我只能显示第一行,因为它一直有第一行的数据。这是因为表单在第一个 php 文件中。然后在我提交表单后,它被定向到这个文件,并且它不断获得第一行。

<?php $con=mysqli_connect("localhost","root","","task");?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>

<?php
$id=$row['id'];
 echo '&nbsp; &nbsp; &nbsp; &nbsp;';
    echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $row['title'] . '</button>';
?>
<?php
}?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<div class="note" data-id="<?= $row['id'] ?>">
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">

<?php
   echo '<br><br>';

       echo '<div class="padding">'.$row['title'].'';
        echo '<br><br><br><br>';
    echo ''.$row['note'].'</div>';
?>
</div>
</div>
<?php
}?>
<?php
<?php
function test_input($data) { 
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
} ?>

标签: javascriptphploops

解决方案


下面的代码未经测试,但它应该是正确的,并且可以让您更好地了解您在做什么对/错。我希望它有帮助..

<?php 
$con=mysqli_connect("localhost","root","","task");
$results = mysqli_query($con, "SELECT * FROM note");



while ($row = mysqli_fetch_array($results)) { //starting your data row loop


$id=$row['id'];// you are creating a variable here but not using it two lines down. 
 echo '&nbsp; &nbsp; &nbsp; &nbsp;';
 //YOUR OLD CODE echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $row['title'] . '</button>';
 echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $id . '</button>';// use the variable "$id" that you created here

// You dont need the next two lines, you already did a query and have the data loaded in the "$results" array
/*  $results = mysqli_query($con, "SELECT * FROM note"); 
 while ($row = mysqli_fetch_array($results)) */

?> // close the php tag if you are going to switch to html instead of "echoing html"
/* OLD CODE <div class="note" data-id="<?= $row['id'] ?>"> you were missing the "php" in the php tags */
<div class="note" data-id="<?php echo $id; ?>"> // corrected code
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">

<?php //switching back to php so create your open tag again...
   echo '<br><br>';

       echo '<div class="padding">'.$row['title'].'';
        echo '<br><br><br><br>';
    echo ''.$row['note'].'</div>';// you dont NEED the '' before .$row....  unless you want it but its just essentially a blank string
?>
</div>
</div>
<?php
} // ending your "for each data row" here 
?>

<?php
//  PS you're not using this function anywhere unless its in ommited code?
function test_input($data) { 
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
} ?>

推荐阅读