javascript - 如何显示数据库中的每一行数据?
问题描述
我很难在数据库的每一行上显示标题和注释。我想在从页面标题中的表单到显示行数据页面的每一行之后显示一行(带有标题和注释)。
这是我下面的代码:
// 假设我们有 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 ' ';
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;
} ?>
解决方案
下面的代码未经测试,但它应该是正确的,并且可以让您更好地了解您在做什么对/错。我希望它有帮助..
<?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 ' ';
//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;
} ?>
推荐阅读
- php - 有没有办法判断用户是否点击了我的谷歌广告?
- python - Keras 模型错误,引用不兼容的目标形状,但模型摘要并未表明它是错误的
- r - 将信息添加到有条件地比较字符串的列
- spring - Spring security Oauth2 客户端凭据水平扩展
- c# - 在 WPF TreeView 中将 ToolTip 添加到 DataTemplate
- c++ - 按值排序 std::map
- python - 尝试在不使用 Replace/Mapç 的情况下使用字符串附加列表元素
- python - Python 'Logger' 模块双重记录
- python-3.x - Improve speed of wikipedia.search with Python
- go - Add struct or pointer to struct to slice