如何显示数据库中的每一行数据?

时间:2019-01-30 15:16:07

标签: javascript php loops

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

这是我的下面的代码:

// 假设我们有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;
} ?>

2 个答案:

答案 0 :(得分:0)

在循环中,您使用mysqli_fetch_array,它返回一个数组,该数组中的每个元素都包含字段值。

您想要的是mysqli_fetch_assoc,它将返回一个哈希,然后您就可以使用现在使用的哈希了。

另一件事是,您不需要在那里有2个循环来查询数据库。并且请缩进您的代码,这会使您和其他所有人都很难阅读它。

下面是你的代码的更新/清理版本。这已经过测试,您可以找到一个示例代码,其中包含在我的Github here上运行的说明。

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

while ($row = mysqli_fetch_assoc($results)) {
  $id = $row['id'];
  echo '&nbsp; &nbsp; &nbsp; &nbsp;';
  echo '<button class="call_modal" data-id="' . $id . '" style="cursor:pointer;">'. $row['title'] . '</button>';
  ?>
  <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'];
          echo '</div>'
          ?>
        </div>
      </div>
    </div>
  </div>
<?php
}
?>

<?php
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

答案 1 :(得分:0)

下面的代码未经测试,但应正确无误,可让您更好地了解所做的对与错。希望对您有所帮助。

<?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;
} ?>