我正在尝试在 MySql 中显示上传到我的“商店”表的最后 5 张图片。我对 PHP 和数据库完全是个菜鸟,我已经阅读了很多关于如何做到这一点的文章,但没有运气。
我可以一次存储和显示一张图片,但我希望能够有一个画廊来显示最近上传的 5 张。
任何建议或帮助将不胜感激谢谢!
ps 我知道将图片存储到这样的数据库是不受欢迎的,但这个项目只是为了练习。
索引.php
<!DOCTYPE html>
<html>
<head>
<title>Project One</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
<form>
<p />
<?php
//connect to database
(connect to server)
(select correct DB)
//file properties
$file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "please select an image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = $_FILES['image']['name'];
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "That's not an image.";
else
{
if (!$insert = mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image')"))
echo "Problem Uploading Image.";
else
{
$lastid = mysql_insert_id();
echo "Image uploaded. <p />Your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>
<p />
<p />
<a href="http://WEBSITE.com/gallery.php"> Go to Gallery </a>
</body>
</html>
获取.php
<?php
//connect to database
(connect to server)
(select correct DB)
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>