Trying to build this page for adding new products,for my client Page with a form - 1)To upload the image 2)Add price and name data
For some reason its not working. After i click on the submit button,nothing happens,its stayens on the 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 into 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 the server");
}
else{
header('Location: upload_image_food.php?cname='.$category.');
}
}
?>;
</div>
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 into products ('product_name','product_price','product_pic','category') VALUES ('".$product_name."', '".$product_price."', '".$image_name."', '".$category."')";
To
$query_upload = "INSERT into 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 it is success then, it need to go to desired location with some message. This is the reason why it's showing blank.
4) Change
}else{
exit("Error While uploading image on the server");
}
else{
header('Location: upload_image_food.php?cname='.$category.');
}
}
?>;
to
}else{
exit("Error While uploading image on the 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 into 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 the server");
}
else
{
header("Location: upload_image_food.php?cname=$category");
}
?>