我正在建立一个使用MySQL数据库的画廊(是的,我知道这是一个不好的做法,但它是当下的要求.)我可以上传多个图像,但我无法显示存储在数据库中的所有图像. FORM允许上传五张图片.然后,用户必须前进到另一个页面,其中数据库中的所有图像(包括最近上载的图像)将与图像的描述一起显示.我已经有代码但是在显示器上工作的代码不起作用或我认为是错误的.
这是表单代码:
Upload imageAll forms must be filled.
File:
这是要上传的脚本:
//connect to the database//
$con = mysql_connect("localhost","root", "");
if(!$con)
{
die('Could not connect to the database:' . mysql_error());
echo "ERROR IN CONNECTION";
}
$sel = mysql_select_db("imagedatabase");
if(!$sel)
{
die('Could not connect to the database:' . mysql_error());
echo "ERROR IN CONNECTION";
}
//file properties//
$file = $_FILES['image']['tmp_name'];
echo '
';
/*if(!isset($file))
echo "Please select your images";
else
{
*/for($count = 0; $count < count($_FILES['image']); $count )
{
//$image = file_get_contents($_FILES['image']['tmp_name']);
$image_desc[$count] = addslashes($_POST['imageDescription'][$count]);
$image_name[$count] = addslashes($_FILES['image]']['name'][$count]); echo '
';
$image_size[$count] = @getimagesize($_FILES['image']['tmp_name'][$count]);
$error[$count] = $_FILES['image']['error'][$count];
if($image_size[$count] === FALSE || ($image_size[$count]) == 0)
echo "That's not an image";
else
{
// Temporary file name stored on the server
$tmpName[$count] = $_FILES['image']['tmp_name'][$count];
// Read the file
$fp[$count] = fopen($tmpName[$count], 'r');
$data[$count] = fread($fp[$count], filesize($tmpName[$count]));
$data[$count] = addslashes($data[$count]);
fclose($fp[$count]);
// Create the query and insert
// into our database.
$results = mysql_query("INSERT INTO images( description, image) VALUES ('$image_desc[$count]','$data[$count]')", $con);
if(!$results)
echo "Problem uploding the image. Please check your database";
//else
//{
echo "";
//$last_id = mysql_insert_id();
//echo "Image Uploaded.
";//header('Lcation: display2.php?id=$last_id');
}
//}
}
mysql_close($con);
header('Location: fGallery.php');
?>
最后应该显示的那个:
//connect to the database//
mysql_connect("localhost","root", "") or die(mysql_error());
mysql_select_db("imagedatabase") or die(mysql_error());
//requesting image id
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM images WHERE id = $id");
while($datum = mysql_fetch_array($image, MYSQL_ASSOC))
{
printf("Description %s $image = $image['image'];
header("Content-type: image/jpeg");
}
mysql_close();
?>
非常感谢您的帮助.继续前进,我非常需要它.
解决方法:
据我所知,你的帖子是上传和存储不是问题,但显示图像是.这可能是因为您使用的是未设置的变量,因此无法在数据库中找到任何结果.如果我误解了,请告诉我.
// No ID
$image = mysql_query("SELECT * FROM images ORDER BY id DESC");
?>
另请参阅Prof83所说的内容.如果您的脚本仅使用一个图像,请忽略我的帖子.
最后但并非最不重要的是,如果您使用不同的文件类型,还要在标头中回显正确的MIME格式.
更新
我结合了两个答案.
编辑你的循环:
while($row = mysql_fetch_assoc($image))
{
echo '';
}
?>
创建页面名称img.php
$query = mysql_query("SELECT image FROM images WHERE id = ".$_GET['id']);
$row = mysql_fetch_assoc($query);
header("Content-type: image/jpeg");
echo $row['image'];
?>
来源:https://www.icode9.com/content-2-468851.html