PHP,上传的图片网址未存储在数据库中。我哪里错了?

时间:2016-05-08 19:57:04

标签: php mysql

我为管理员创建了一个表单,用于上传产品详细信息。除图像文件路径外,所有字段都正确存储。有人能告诉我哪里出错了吗?

图像存储在images文件夹中,但路径未存储在数据库中。

形式:

<form action="form.php" method="post" enctype="multipart/form-data" id="inventoryList" name="inventoryList">
<table width="90%" border="0" cellspacing="0" cellpadding="6">
    <tr><td width="20%" align="right"><h3>Products Table</h3></td></tr>
    <!-- item_code -->
  <tr>
    <td width="20%" align="right">Item Code</td>
    <td width="80%"><label>
      <input name="item_code" type="text" id="item_code" size="20" />
    </label></td>
  </tr>
    <!-- item_name -->
  <tr>
    <td width="20%" align="right">Product Name</td>
    <td width="80%"><label>
      <input name="item_name" type="text" id="item_name" size="64" />
    </label></td>
  </tr>
  <!-- brand_name -->
  <tr>
    <td width="20%" align="right">Brand Name</td>
    <td width="80%"><label>
      <input name="brand_name" type="text" id="brand_name" size="20" />
    </label></td>
  </tr>
    <!-- model_number -->
  <tr>
    <td width="20%" align="right">Model Number</td>
    <td width="80%"><label>
      <input name="model_number" type="text" id="model_number" size="20" />
    </label></td>
  </tr>
  <!-- weight -->
  <tr>
    <td width="20%" align="right">Weight</td>
    <td width="80%"><label>
      <input name="weight" type="text" id="weight" size="20" />
    </label></td>
  </tr>
  <!-- dimension -->
  <tr>
    <td width="20%" align="right">Dimension</td>
    <td width="80%"><label>
      <input name="dimension" type="text" id="dimension" size="20" />
    </label></td>
  </tr>
  <!-- description -->
  <tr>
    <td align="right">Product Description</td>
    <td><label>
      <textarea name="description" id="description" cols="64" rows="5"></textarea>
    </label></td>
  </tr>
  <tr>
  <!-- category -->
    <td align="right">Category</td>
    <td><label>
      <select name="category" id="category">
      <option value="CellPhone">Smart Phone</option>
      <option value="Laptop">Laptop</option>
      </select>
    </label></td>
  </tr>
  <!-- quantity -->
  <tr>
    <td width="20%" align="right">Quantity</td>
    <td width="80%"><label>
      <input name="quantity" type="text" id="quantity" size="20" />
    </label></td>
  </tr>
  <tr>
  <!-- price -->
    <td align="right">Product Price &euro;</td>
    <td><label>
      <input name="price" type="text" id="price" size="12" />
    </label></td>
  </tr>
  <!-- image -->
  <tr>
    <td align="right">Product Image</td>
    <td><label>
      <input type="file" name="imagename" id="imagename" />
    </label></td>
  </tr> 
  <tr>
    <td>&nbsp;</td>
    <td><label>
      <input type="submit" name="submit" id="submit" value="Add This Item Now" />
    </label></td>
  </tr>
</table>

PHP脚本:

if(isset($_POST['submit'])){
    $random_name = rand();
    $sql1="INSERT INTO products
    (item_code, item_name, brand_name, model_number, weight, dimension, description, category, quantity, price, imagename) 
    VALUES 
    ('$_POST[item_code]','$_POST[item_name]','$_POST[brand_name]',
    '$_POST[model_number]', '$_POST[weight]', '$_POST[dimension]', '$_POST[description]','$_POST[category]', 
    '$_POST[quantity]','$_POST[price]', '$_POST[imagename]')";

    mysqli_query($connect,$sql1);

    move_uploaded_file($_FILES['imagename']['tmp_name'], "../images/$random_name.jpg");
    header("location: inventory.php"); 
    exit(); 
}

显示缺少文件路径的图片:

enter image description here

3 个答案:

答案 0 :(得分:1)

$ _ POST [imagename] 根本不存在,因为该图片不在 $ _ POST 变量中,而是在 $ _ FILES 变量中

所以,你必须这样做有点不同:

    <?php
        if(isset($_POST['submit'])) {
            $random_name = rand();
            $sql1        = "INSERT INTO products
                        (item_code, item_name, brand_name, model_number, weight, dimension, description, category, quantity, price, imagename) 
                        VALUES 
                        ('$_POST[item_code]','$_POST[item_name]','$_POST[brand_name]',
                        '$_POST[model_number]', '$_POST[weight]', '$_POST[dimension]', '$_POST[description]','$_POST[category]', 
                        '$_POST[quantity]','$_POST[price]', ";  //'$_POST[imagename]')

            //FIRST UPLOAD THE IMAGE AND IF THAT WORKS; THEN ADD THE IMAGE URL TO YOUR QUERY:
            if(move_uploaded_file($_FILES['imagename']['tmp_name'], "../images/$random_name.jpg")){ 
                // APPEND THE IMAGE URL TO THE QUERY & EXECUTE IT
                $sql1   .=  "../images/{$random_name}.jpg" . ")"; 
                mysqli_query($connect, $sql1);          
            }
            header("location: inventory.php");
            exit();
        }

答案 1 :(得分:0)

问题在于您尝试插入数据库的值。

您无法插入$_POST["imagename"],因为它不存在。您尚未使用$_POST超全局发布图片名称以及表单的其余部分,而是使用代码段顶部的代码{{1}来实时创建图片名称}。

因此,您需要将 的结果插入数据库,而不是$random_name = rand()

以下是您的新$_POST["imagename"]声明:

INSERT INTO

答案 2 :(得分:0)

$random_name = rand(); $sql1="INSERT INTO products (item_code, item_name, brand_name, model_number, weight, dimension, description, category, quantity, price, imagename) VALUES ('$_POST[item_code]','$_POST[item_name]','$_POST[brand_name]', '$_POST[model_number]', '$_POST[weight]', '$_POST[dimension]', '$_POST[description]','$_POST[category]', '$_POST[quantity]','$_POST[price]', '$random_name')"; 数组中没有“imagename item”,因为您将其像文件一样发送。

试试这个:

post