gpt4 book ai didi

PHP 数据从 MySQL 到每个条目的单独页面

转载 作者:行者123 更新时间:2023-11-29 22:58:31 25 4
gpt4 key购买 nike

我正在将信息从数据库提取到 .php 页面中。我希望这些信息作为单独的条目(所有问题/答案放在一起,然后是下一组答案)而不是放在表格中。我的表格很长,因此将所有内容都放在表格中,就像我一样,它会变得非常长。我该如何改变这个?我知道我已将其设置在表中,但我不确定如何将其更改为单独的项目。

更好的(尽管我怀疑非常困难)是让用户单击仅显示 ID 和 date_visit 的行,这会将他们带到包含已完成的表单结果的单独页面。因此,几乎可以将每个表单视为一个单独的页面。我预计这可能会非常复杂?

我对使用数据库非常陌生。这是我的代码:

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM survey";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Store Name</th><th>Receipt #</th><th>Date of Store Visit</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["ID"]."</td><td>".$row["storename"]."</td><td>".$row["receipt"]."</td><td>".$row["date_visit"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>

最佳答案

好吧,我不确定你在做什么,你的集合似乎只包含 4 条数据,所以为什么你想要点击查看剩余的 2 列,这超出了我的理解。

这就是您基本上要做的事情(注意:我还没有测试过其中任何一个,因此可能存在一些语法错误 - 它不是剪切/粘贴代码,但它应该说明您的基础知识要求做)...

因此 list.php 本质上与您现在拥有的相同,只是我们将添加一个链接,正如我在评论中提到的那样:

<?php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// only selecting the fields you need is generally better
// its also a good practice to get into aliasing your tablenames
// and always using the table_or_alias.column_name to reference them
$sql = "SELECT s.ID, s.date_vist FROM survey s";

$result = $conn->query($sql);

if ($result === false) {
die(sprintf(
'An error occurred attempting to access the data: "%s"',
$conn->error
));
}
?>

<?php if ($result->num_rows > 0): ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>Date of Store Visit</th>
<th>&nbsp;</th>
</tr>
</thead>
<tbody>
<?php while (false !== ($row = $result->fetch_assoc())): ?>
<tr>
<td><?php echo $row['ID'] ?></td>
<td><?php echo $row['date_visit'] ?></td>
<td>
<?php pritntf(
'<a href="view.php?id=%s">View Details</a>',
echo $row['ID']
); ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php else: ?>
<p>0 Results</p>
<?php endif; ?>

<?php $conn->close(); ?>

现在,对于 view.php,您将从 URL 中获取参数 id 并使用它从数据库中选择完整信息。

<?php

$id = isset($_GET['id']) ? (integer) $_GET['id'] : null;
if (null === $id) {
header("HTTP/1.0 404 Not Found", true, 404);
exit;
}

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// we will us a prepared statment to avoid SQL injection
// when use a ? to mark a placeholder for a value in the query
$sql = 'SELECT s.* FROM survey s WHERE s.ID = ? LIMIT 1';
$stmt = $conn->prepare($sql);

if (false === $stmt) {
die(sprintf(
'Error attempting to access data: "%s"',
$conn->error
));
}

// bind the ID to the prepared statement
$stmt->bind_param('i', $id);

if (!$stmt->execute() || false === ($result = $stmt->get_result())) {
die(sprintf(
'Error attempting to access data: "%s"',
$stmt->error
));
} elseif ($result->num_rows < 1) {
// no results so 404
header("HTTP/1.0 404 Not Found", true, 404);
exit;

} else {
$survey = $result->fetch_assoc();
$labels = array(
'ID' => 'ID',
'storename' => 'Store Name',
'receipt' => 'Receipt #',
'date_visit' => 'Date of Store Visit'
);
}

$stmt->close();
$result->close();
?>


<table>
<tbody>
<?php foreach($survey as $column => $value): ?>
<tr>
<th><?php echo isset($labels[$column])
? $labels[$column]
: ucwords($column); ?>
</th>
<td><?php echo $value; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>

关于PHP 数据从 MySQL 到每个条目的单独页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28614682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com