PHP将信息上传到数据库中

时间:2014-01-25 12:50:59

标签: php sql image

我正在尝试将有关项目的一些信息上传到数据库中,并将图像上传到文件夹中。我没有收到任何错误,但sql查询没有运行,图像也没有上传。

    <?php

    include'connect.php';

    if( isset($_POST['item_name']) && isset($_POST['description']) && isset($_POST['price']) && isset($_POST['quantity']) && isset($_POST['manufacturer']) &&     isset($_POST['pic']) )
    {
        $name = $_FILES['pic']['name'];
    $tmp_name = $_FILES['pic']['tmp_name'];

    if(!empty($_POST['item_name']) && !empty($_POST['description']) &&  !empty($_POST['price']) && !empty($_POST['quantity']) && !empty($_POST['manufacturer']) &&  !empty($name) )
    {   
        $item_name = $_POST['item_name'];
        $description = $_POST['description'];
        $price = $_POST['price'];
        $quantity = $_POST['quantity'];
        $manufacturer = $_POST['manufacturer'];

        $location = 'Upload/';
        move_uploaded_file($tmp_name, $location.$name);

        $query = "INSERT INTO `item` VALUES ('$item_name', '$description','$price','$quantity','$name','$manufacturer')";

        if($query_run = mysql_query($query))
        {   echo 'Item successfully added.';    }

        else
        {   echo 'Query Failure';   }


    }
}

?>

<form  action = 'additems.php' method = 'POST' enctype = 'multipart/form-data' >
    Name: <input type = 'text' name = 'item_name' ><br><br>
    Description: <input type = 'text' name = 'description' ><br><br>
    Price: <input type = 'text' name = 'price' ><br><br>
    Quantity: <input type = 'text' name = 'quantity' ><br><br>
    Manufacturer: <select name = 'manufacturer' >
                  <option value = '1'> Manufacturer 1 </option>
                  <option value = '2'> Manufacturer 2 </option>
                  <option value = '3'> Manufacturer 3 </option>
                  <option value = '4'> Manufacturer 4 </option>
                  </select>
                  <br><br>
    Picture: <input type = 'file' name = 'pic' > <br><br>

    <input type = 'submit' value = 'Submit' >

</form>

我有一个包含4个条目的制造商表。 ids范围从1-4。 connect.php文件将我连接到数据库,我可以确认它没有错误。

1 个答案:

答案 0 :(得分:0)

更改

 isset($_POST['pic'])

 isset($_FILE['pic']['name'])