php - Upload image and insert data to db issue -
trying build page adding new products,for client page form - 1)to upload image 2)add price , name data
for reason not working. after click on submit button,nothing happens,its stayens on saveimage.php page
<form action="saveimage.php" enctype="multipart/form-data" method="post"> <input name="name" type="text">שם:</br> <input name="price" type="text">מחיר:</br> <input name="category" value="<?php $category ?>" type="hidden"> <input name="uploadedimage" type="file"> <input name="new_product" type="submit" value="upload image"> </form>
the php page
<?php include("../../dbconn.php"); function getimageextension($imagetype) { if(empty($imagetype)) return false; switch($imagetype) { case 'image/bmp': return '.bmp'; case 'image/gif': return '.gif'; case 'image/jpeg': return '.jpg'; case 'image/png': return '.png'; case 'jpeg': return '.jpg'; case 'png': return '.png'; case 'jpg': return '.jpg'; default: return false; } } if (!empty($_files["uploadedimage"]["name"])) { $file_name=$_files["uploadedimage"]["name"]; $temp_name=$_files["uploadedimage"]["tmp_name"]; $imgtype=$_files["uploadedimage"]["type"]; $ext= getimageextension($imgtype); $imagename=date("d-m-y")."-".time().$ext; $target_path = "img/".$imagename; } if(isset($_post['new_product'])) { if(isset($_post['name']) && isset($_post['price'])) { $product_name = strip_tags($_post['name']); $product_price = strip_tags($_post['price']); $category = strip_tags($_post['category']); } if(move_uploaded_file($temp_name, $target_path)) { $query_upload="insert products ('product_name','product_price','product_pic','category') values ('".$product_name."', '".$product_price."', '".$image_name."', '".$category."')"; mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); }else{ exit("error while uploading image on server"); } else{ header('location: upload_image_food.php?cname='.$category.'); } } ?>;
few changes.
1) change
<input name="category" value="<?php $category ?>" type="hidden">
to
<input name="category" value="<?php echo $category; ?>" type="hidden">
2) change (remove single quotes '
around column name)
$query_upload="insert products ('product_name','product_price','product_pic','category') values ('".$product_name."', '".$product_price."', '".$image_name."', '".$category."')";
to
$query_upload = "insert products (product_name,product_price,product_pic,category) values ('$product_name', '$product_price', '$image_name','$category')";
3) add header("location: upload_image_food.php?cname=$category&message=success");
inside move_uploaded_file
because when success then, need go desired location message. reason why it's showing blank.
4) change
}else{ exit("error while uploading image on server"); } else{ header('location: upload_image_food.php?cname='.$category.'); } } ?>;
to
}else{ exit("error while uploading image on server"); } } else{ header("location: upload_image_food.php?cname=$category"); } ?>
somepage.php
<form action="saveimage.php" enctype="multipart/form-data" method="post"> <input name="name" type="text">שם:</br> <input name="price" type="text">מחיר:</br> <input name="category" value="<?php echo $category ?>" type="hidden"> <input name="uploadedimage" type="file"> <input name="new_product" type="submit" value="upload image"> </form>
saveimage.php
function getimageextension($imagetype) { if(empty($imagetype)) return false; switch($imagetype) { case 'image/bmp': return '.bmp'; case 'image/gif': return '.gif'; case 'image/jpeg': return '.jpg'; case 'image/png': return '.png'; case 'jpeg': return '.jpg'; case 'png': return '.png'; case 'jpg': return '.jpg'; default: return false; } } if (!empty($_files["uploadedimage"]["name"])) { $file_name=$_files["uploadedimage"]["name"]; $temp_name=$_files["uploadedimage"]["tmp_name"]; $imgtype=$_files["uploadedimage"]["type"]; $ext= getimageextension($imgtype); $imagename=date("d-m-y")."-".time().$ext; $target_path = "img/".$imagename; } if(isset($_post['new_product'])) { if(isset($_post['name']) && isset($_post['price'])) { $product_name = strip_tags($_post['name']); $product_price = strip_tags($_post['price']); $category = strip_tags($_post['category']); } if(move_uploaded_file($temp_name, $target_path)) { $query_upload = "insert products (product_name,product_price,product_pic,category) values ('$product_name', '$product_price', '$image_name','$category')"; mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); header("location: upload_image_food.php?cname=$category&message=success"); } else { exit("error while uploading image on server"); } else { header("location: upload_image_food.php?cname=$category"); } ?>
Comments
Post a Comment