Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

mysqli - Uploading Image in MySQL database PHP Error

The if statement is not running .It works fine with the tutorial I have followed but when i have implemented it in my code it fails.The error is Undefined index: thumbnailPic, The HTML code is :

<label >Thumbnail Picture<text>*</text></label><br>
<input type="file" name="thumbnailPic" id="pic"><br>
<label >Original Picture<text>*</text></label><br>
<input type="file" name="originalPic" id="pic"><br>

The PHP code is :

if (is_uploaded_file($_FILES['thumbnailPic']['tmp_name']) != false) {
   $spID="NULL";
   $Quant=$_POST['quantity'];
   $Siz=$_POST['Size'];
   $imgfp = fopen($_FILES['thumbnailPic']['tmp_name'],'rb');
   $stmt = $connection->prepare("INSERT INTO stitchedproduct(sp_id,quantity,size,p_id,color_id,sp_thumbnail,sp_OriginalPic) VALUES (? ,?, ?, ?,?,?,?)");
   $stmt->bindParam(1, $spID);
   $stmt->bindParam(2, $Quant);
   $stmt->bindParam(3, $Siz);
   $stmt->bindParam(4, $ProductID);
   $stmt->bindParam(5, $colour);
   $stmt->bindParam(6, $imgfp);
   $stmt->bindParam(7, $imgfp);
   $stmt->execute();
}
else
   echo "Error uploading image";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try something like this,may be this code could help you a bit

$image = addslashes(file_get_contents($_FILE['image']['tmp_name'])); //SQL Injection defence!
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `product_images` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!mysql_query($sql)) { // Error handling
    echo "Something went wrong! :("; 
}

also ur form must look like something this

<form action="insert_product.php" method="POST" enctype="multipart/form-data">
    <label>File: </label><input type="file" name="image" />
    <input type="submit" />
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...