上传图像并将数据插入数据库问题

时间:2016-01-27 15:09:18

标签: php html

尝试为我的客户构建此页面以添加新产品 带表格的页面 - 1)上传图像 2)添加价格和名称数据

由于某种原因,它不起作用。点击提交按钮后,没有任何反应,它在saveimage.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 $category ?>" type="hidden">
 
<input name="uploadedimage" type="file">

<input name="new_product" type="submit" value="Upload Image">
 
</form> 

PHP页面

 <?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.');
    }
} 
?>;

1 个答案:

答案 0 :(得分:0)

几乎没有变化。

1)更改

<input name="category" value="<?php $category ?>" type="hidden">

<input name="category" value="<?php echo $category; ?>" type="hidden">

2)更改(删除列名称周围的单引号'

$query_upload="INSERT into products ('product_name','product_price','product_pic','category') VALUES ('".$product_name."', '".$product_price."', '".$image_name."', '".$category."')";

$query_upload = "INSERT into products (product_name,product_price,product_pic,category) VALUES ('$product_name', '$product_price', '$image_name','$category')";

3)header("Location: upload_image_food.php?cname=$category&Message=Success");内添加move_uploaded_file,因为当它成功时,需要使用某些消息转到所需位置。这就是它显示空白的原因。

<强> 4) 改变

}else{

   exit("Error While uploading image on the server");
} 
else{

   header('Location: upload_image_food.php?cname='.$category.');
    }
} 
?>;

}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");
}
?>