我创建了一个包含ID,描述,关键字,年份和位置的数据库。我正在创建一个将填充所有上传到我的数据库的页面。为了清楚起见,我已将上传的图像保存到服务器上的文件夹中,而不是我的数据库中。
我可以设法获取包含数据库字段的表格,但现在我试图将与图像ID相对应的图像放入表格中。
我相信我需要做一些像
这样的事情$query = "SELECT * FROM insert image path here
我尝试过不同的东西,但却无法发挥作用。
P.S。我现在正在使用MAMP,而不是实际将其上传到服务器。
以下是我的代码:
<html>
<body>
<?php
error_reporting(-1);
// connection
// TODO:secure connection details
$servername = "localhost";
$username = "name";
$password = "password";
$dbname = "archive";
$dirname = "uploads/";
// Create db connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// stop if failed
if (mysqli_connect_errno()) {
die("Connection failed: " .
mysqli_connect_error() .
" " . mysqli_connect_errno());
}
// I think I need to call the img before the data
$query = "SELECT * FROM $target_file = "uploads/img_".$id.".jpg";"
// TODO: fix SQL injection
$query = "SELECT * FROM photo_data"; // give me everything from photodata table ordered desc
$result = mysqli_query($conn, $query);
if (!$result){
die("Database query failed." . mysqli_error($conn));
} else {
echo "<table border='1'>";
echo "<tr><th>id</th><th>uniqueid</th><th>description</th><th>keywords</th><th>year</th><th>location</th><th>target file</th></tr>";
//building row by adding values
while($row = mysqli_fetch_row($result)){
echo "<tr>";
//building columns
foreach ($row as $key => $value) {
echo "<td>",$value,"</td>";
}
// I think I need to add my images in the section above
echo "</tr>";
}
echo "</table>";
}
mysqli_close($conn);
?>
</body>
</html>
答案 0 :(得分:1)
因为您没有将图像保存在数据库中,而是在服务器上保存,因此您不必为此准备SQL查询。您实际需要做的是查看文件系统以查找特定图像。据我所知,你想在表格中显示图像。你可以这样做:
<pre>from random import shuffle
myList = ["file1", "file2", "file3", "file4"]
longList = []
for x in range(0,10):
shuffle(myList)
longList.append(myList)
print(longList)<code>