簡體   English   中英

如何顯示數據庫中的每一行數據?

[英]How to display each row data in the database?

我很難在數據庫的每一行上顯示標題和注釋。 我想在從頁面標題中的表格到顯示行數據頁面的每一行之后顯示一行(包括標題和注釋)。

這是我的代碼如下:

//假設我們有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;
} ?>

在循環中,您使用mysqli_fetch_array ,它返回一個數組,該數組中的每個元素都包含字段值。

您想要的是mysqli_fetch_assoc ,它將返回一個哈希,然后您可以使用現在使用的方式。

另一件事是,您不需要在那里有兩個查詢數據庫的循環。 並且請縮進您的代碼,這使您和其他所有人都很難閱讀它。

這是代碼的更新/清理版本。 這已經過測試,您可以在此處找到示例代碼以及在我的Github上運行的說明。

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

以下代碼未經測試,但應正確無誤,可讓您更好地了解所做的對/錯。 希望對您有所幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM