【问题标题】:How to display each row data in the database?如何显示数据库中的每一行数据?
【发布时间】:2019-01-30 15:16:06
【问题描述】:

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

下面是我的代码:

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

【问题讨论】:

  • 您不需要在每一行都添加&lt;?php ... ?&gt;!如果您不这样做,将使代码更易于阅读和调试
  • 嗨帕特里克,除了几个丢失/不必要的 php 标签之外,您的代码看起来应该可以工作...您能否提供更多有关您遇到的错误或什么/没有发生的详细信息?
  • 假设我们有4行数据,我的代码,我只能显示第一行,因为它一直有第一行数据,因为表单在第一个php文件中,然后在我提交表单后,它被定向到该文件并不断获取第一行
  • 是的,抱歉,我在这里忽略了一个会导致问题的关键行。我正在整理一些示例代码,以帮助您了解问题所在。等一下……

标签: javascript php loops


【解决方案1】:

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

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

【讨论】:

  • 我会尽快尝试,因为我有课,我只是更新一个评论
【解决方案2】:

在您的循环中,您使用的是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;
}

【讨论】:

  • 我会尽快尝试,因为我有课,我会更新评论
  • 没问题,如果有帮助请告诉我
  • 老兄,我有一个问题,我得到了所有按钮的最后一行数据中的所有数据
  • 它让我在我的数据库中保留所有模式的最后一行数据
  • SIr,它不起作用,因为我一直在获取数据库中的最后数据
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2019-03-14
  • 2016-12-19
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-01-06
相关资源
最近更新 更多