我是 php 和 mysqli 的初学者。我想编写一个程序,显示数据库表中的名称列表。允许用户单击名称,单击名称应显示该人的图片。
我的代码如下。我可以向数据库添加名称和人员并检索数据库中的所有名称,但我不知道如何使名称“可点击”,以便用户可以单击它们并显示图片。有人可以看看并给我一些提示吗?谢谢你的帮助!
请参阅下面的部分代码 - 这是我的主要 php 文件:
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("blah blah", "username","pw", "blah");
if($mysqli->connect_errno){
echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script>$(document).ready(function() {$("form").validate();});</script>
<title>CS302</title>
<meta charset="UTF-8">
<p></p>
</head>
<body>
<div>This is a homework</div>
<p></p>
<div>
<form method="post" action="index.php" enctype="multipart/form-data">
Celebrity Name: <input type="text" name="c_name">
Celebrity Photo: <input type="file" name="c_picture">
<input type="submit" name="add" value="Upload">
</form>
</div>
<br>
<div>
<table>
<tr>
<td>See below for a list of celebrities in our database</td>
</tr>
<?php
//Display names in the celebrity database
if(!($stmt = $mysqli->prepare("SELECT c_name FROM celebrity"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($c_name)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo "<tr>\n<td>\n" . $c_name . "\n</td>\n</tr>";
}
$stmt->close();
?>
</table>
</div>
</body>
</html>
这是用于检索图像的 php 文件:
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysqli("blah", "blah","blah", "blah");
if($mysqli->connect_errno){
echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//retrieve the blob
if(!($stmt = $mysqli->prepare("SELECT c_picture FROM celebrity where c_name = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("i",$_POST['c_id']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($c_picture)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
$stmt->store_result();
while($stmt->fetch()){
echo "<tr>\n<td>\n" . $c_picture . "\n</td>\n</tr>";
}
$stmt->close();
?>