我在需要显示数据库内容的网页上工作,让您点击它们以显示与该ID相关的数据库中的更多信息,甚至将内容旁边的复选框链接到相同的数据库ID我可以删除帖子,如果需要,我的代码显示到目前为止完美的内容,
但我不知道如何将与id相关的id / info链接到复选框或onclick命令
到目前为止我的代码:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Get our database connector
require("includes/conn.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="featured">
<h2></h2>
<div>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Grab the data from our people table
$sql = "SELECT * FROM people ORDER BY ID";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo "<div class=\"picture\">";
echo "<p>";
// Note that we are building our src string using the filename from the database
echo "<img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" height=\"219\" width=\"350\" /><br />" . "<br />";
echo $row['fname'] . " " . "<br />" . "<br />";
echo "</p>";
echo "</div>";
}
?>
</div>
</div>
</body>
</html>
答案 0 :(得分:2)
无需复选框,您只需使用锚点
包装图像即可然后,在其中放入一个包含查询字符串中id的URL:
echo "
<a href='moreinfo.php?id=".$row['id']."'>
<img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" height=\"219\" width=\"350\" />
</a>
<br />" . "<br />";
echo $row['fname'] . " " . "<br />" . "<br />";
强制性说明:
Please, don't use
mysql_*
functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial。
现在,在使用PDO的moreinfo.php
内:
<?php
$results = array();
if(isset($_GET['id'])) {
$id = $_GET['id'];
$db = new PDO('mysql:host=localhost;dbname=DATABASE_NAME', 'username', 'password');
$sql = 'SELECT * FROM people WHERE `id` = :id';
$select = $db->prepare($sql);
$select->bindParam(':id', $id, PDO::PARAM_INT);
$select->execute();
$results = $select->fetch(PDO::FETCH_ASSOC);
} else {
header('Location: go_back_to_index.php');
}
?>
<?php if(!empty($results)): ?>
<h1>Results</h1>
<table cellpadding="10">
<tr>
<td>First Name:</td><td><?php echo $results['fname']; ?></td>
</tr>
<tr>
<td>Last Name:</td><td><?php echo $results['lname']; ?></td>
</tr>
<tr>
<td>Photo: </td><td><img src="content/uploads/<?php echo $results['filename']; ?>" alt="profile pricture" /></td>
</tr>
<!-- and so on -->
</table>
<?php endif; ?>