如何根据上传表单将图像插入数据库

时间:2014-01-16 13:31:43

标签: php

我想基于上传表格将图片插入数据库。

我的查询有效,能够在我提交表单时插入除图像之外的所有字段 但是,当我更改我的代码以将图像添加到数据库中时, 代码不再起作用,它不会将我的字段插入数据库。

有人可以告诉我,如果在点击提交按钮后将图像插入数据库,我该怎么办?

  <?php
if(isset($_POST['description'])){
    $product_name = mysql_real_escape_string($_POST['product_name']);
    $product_price = mysql_real_escape_string($_POST['product_price']);
    $category = mysql_real_escape_string($_POST['category']);
    $subcategory = mysql_real_escape_string($_POST['subcategory']);
    $product_description = mysql_real_escape_string($_POST['description']);
    $product_package = mysql_real_escape_string($_POST['product_package']);
    $product_image = mysql_real_escape_string($_POST['product_image']);




$sql = mysql_query("SELECT id FROM supermarket WHERE description='$product_description' LIMIT 1");
$productMatch = mysql_num_rows($sql);
if($productMatch > 0) {
    echo "Sorry you tried to place a duplicate 'Product Description' into the system, <a href='invertorylist.php'>click here</a>";
    exit();
}

//add name of image hear
$newname = "$product_description.jpg";

//Add this prouct into the database now
$sql = mysql_query ("INSERT INTO supermarket (category,subcategory,name,description,image,packaging,price)
VALUES ('$category','$subcategory','$product_name','$product_description',$product_image','$product_package','$product_price')");




//place image in the folder
move_uploaded_file($_FILES['fileField']['tmp_name'],"images/$newname");
    header("location: inventorylist.php"); 
    exit();

}

1 个答案:

答案 0 :(得分:0)

//add name of image hear
$newname = "$product_description.jpg";

//Add this prouct into the database now 

$sql = mysql_query ("INSERT INTO supermarket (category,subcategory,name,description,image,packaging,price) VALUES ('$category','$subcategory','$product_name','$product_description','$newname','$product_package','$product_price')");


  //place image in the folder

move_uploaded_file($_FILES['fileField']['tmp_name'],"images/$newname");
    header("location: inventorylist.php"); 
    exit(); }