我在购物车上工作,我希望在其中显示存储在数据库中的各自图像的产品。但我的PHP代码没有按预期工作。我想显示存储在1条以上记录中的图像。
这些是我的html和php页面:
的index.html
<html>
<body>
<form action="upload_img.php" method="post" enctype="multipart/form-data">
<center>Select Image to upload:
<br><input type="file" name="image">
<input type="submit" value="upload">
</center>
</form>
<form action="display_img2.php">
<input type="submit">
</form>
</body>
</html>
upload_img.php
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
//file properties
$file = $_FILES['image']['tmp_name'];
if (!isset($file)) {
} else {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size == FALSE) {
echo "not an image";
} else {
if (!$insert = mysql_query("insert into photos values ( ('','$image','$image_name')")) {
echo "problem uploading image";
} else {
echo "image uploaded successfully!!";
/* $lastid=mysql_insert_id();
echo "<img src=display_img.php?id=$lastid>"; */
}
}
}
?>
display_img2.php
<?php
$con = mysqli_connect("localhost", "", "", "test");
$sql = "select * from photos";
$result = mysqli_query($con, $sql);
$cnt = mysqli_num_rows($result);
echo $cnt;
while ($cnt) {
echo "<img src=display_img.php?id=$cnt>";
$cnt--;
}
?>
display_img.php
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("select * from photos where id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-Type: image/jpeg");
echo $image;
?>
答案 0 :(得分:0)
登录表单:
<form action=login.php method="POST" name=image_upload enctype="multipart/form-data" >
username
<input type=text name=username><br>
Select image to upload:<input type="file" name="image" id="fileToUpload">
<input type="submit">
</form>
dbconnect.php
<?php
$servername="localhost";
$username="root";
// $password="";
// create connection
$conn=mysqli_connect($servername,$username,"");
mysqli_select_db($conn,"test");
mysqli_query($conn,$sql);
$sql = "ALTER TABLE `user_image` CHANGE `username` `username` VARCHAR(20) NOT NULL";
mysqli_query($conn,$sql);
$sql = "ALTER TABLE `user_image` CHANGE `userimage` `userimage` VARCHAR(40) NOT NULL";
mysqli_query($conn,$sql);
$sql = "ALTER TABLE `user_image` CHANGE `userid` `userid` INT(11) NOT NULL AUTO_INCREMENT";
mysqli_query($conn,$sql);
echo "<br>done";
?>
的login.php
<?php
include 'dbconnect.php';
//simple upload
$file_name = $_FILES['image']['name'];
$file_tmp = $_FILES['image']['tmp_name'];
$image=$_FILES["file"]["name"];
$image_size=getimagesize($_FILES['image']['tmp_name']);
//echo $image_size;
if($image_size==FALSE)
{
echo ("<SCRIPT LANGUAGE='JavaScript'>window.alert('fill data');</SCRIPT>");
//readfile("login.php");`enter code here`
}
else
{
move_uploaded_file($file_tmp,"upload/".$file_name);
//upload in mysql database
$file = $_FILES['image']['name'];
$image_name = addslashes($_FILES['image']['name']);
$x=$_POST["username"];
$sql="INSERT INTO `test`.`user_image` (username, userimage) VALUES('$x','$image_name')";
if(!$insert=mysqli_query($conn,$sql))
echo "problem uploading image";
else
{
$img="upload/".$file_name;
echo '<img src= "'.$img.'" height=200 width=150>';
}
}
?>
答案 1 :(得分:0)
<form action="login.php" method="POST" name="image_upload" enctype="multipart/form-data" >
</form>
您应该在表单操作和名称中使用双引号。