在上传PHP

时间:2015-09-28 17:31:42

标签: php mysqli

我正在尝试将图片与表单数据一起上传。我将所有信息保存到数据库和图像中。

我的表格:

  <form method="post" action="insert_product_page.php" enctype="multipart/form-data" id="insert_product"> 
  <table width="80%" border="1">
  <tr>
  <th width="49%" align="left" scope="col">Product Title</th>
 <th width="51%" align="left" scope="col"><input name="product_title" type="text" required id="product_title" ></th>
 </tr>
  <tr>
<td align="left">Product Category</td>
  <td align="left"><select name="product_cat" required>
    <option>Select A Category</option>
  <option>Category A</option>
  <option>Category B</option>
  <option>Category C</option>

</select></td>
</tr>
<tr>
<td align="left">Product Brand</td>
<td align="left"><select name="product_brand" required>
  <option>Select A Brand</option>
  <option>Brand A</option>
  <option>Brand B</option>
  <option>Brand C</option>
  <option>Brand D</option>

   </select></td>
  </tr>
  <tr>
    <td align="left">Product Image</td>
    <td align="left"><input name="product_image" type="file" ></td>
   </tr>
   <tr>
   <td align="left">Product Description</td>
    <td align="left"><input name="product_price" type="text" required id="product_price" ></td>
  </tr>
 <tr>
  <td align="left">Product Keyword</td>
   <td align="left"><input name="product_keyword" type="text" required id="product_keyword" ></td>
  </tr>
   <tr>
   <td colspan="2" align="center"><input type="submit" name="insert_post" value="Insert New Product"></td>
   </tr>
   </table>
    </form>

insert_product_page.php:

    <?php
        include 'includes/dbConnect.php';

        $product_cat= mysql_real_escape_string($_POST['product_cat']);
        $product_brand= mysql_real_escape_string($_POST['product_brand']);
        $product_title= mysql_real_escape_string($_POST['product_title']);
        $product_price= mysql_real_escape_string($_POST['product_price']);
        $product_desc= mysql_real_escape_string($_POST['product_desc']);
        $product_keyword= mysql_real_escape_string($_POST['product_keyword']);


            $sql = "INSERT INTO products (product_id, product_cat, product_brand, product_title, product_price, product_desc, product_keywords) VALUES (NULL,'$product_cat', '$product_brand', '$product_title', '$product_price', '$product_desc', '$product_keyword')";

            $id = mysqli_insert_id();
            // Place image in the folder 
            $newname = "$id.jpg";
            move_uploaded_file( $_FILES['product_image']['tmp_name'], "images/$newname");


            if (mysqli_query($con, $sql)) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($con);
            }

            $con->close();
    ?>

dbConnect.php:

            <?php
        error_reporting(E_ERROR);

        $servername = 'localhost';
        $username = 'root';
        $password = '';
        $database = 'ecommerce';

        // Create connection
        $con = mysqli_connect($servername, $username, $password,$database);

        // Check connection
        if (!$con) {
            die("Connection failed: " . mysqli_connect_error());
        }
        //echo "Connected successfully";
        ?>

数据已成功保存到数据库中,图像正在上传到文件夹,但图像没有名称,并替换上次上传的图像。我正在尝试上传具有相同名称的图像,保存在数据库中的auto_incremented id。请指导我在哪里做错了。提前致谢

2 个答案:

答案 0 :(得分:0)

当你实际运行查询时,你的问题就在于它。

请试试这个:

<?php
        include 'includes/dbConnect.php';

        $product_cat= mysql_real_escape_string($_POST['product_cat']);
        $product_brand= mysql_real_escape_string($_POST['product_brand']);
        $product_title= mysql_real_escape_string($_POST['product_title']);
        $product_price= mysql_real_escape_string($_POST['product_price']);
        $product_desc= mysql_real_escape_string($_POST['product_desc']);
        $product_keyword= mysql_real_escape_string($_POST['product_keyword']);


            $sql = "INSERT INTO products (product_id, product_cat, product_brand, product_title, product_price, product_desc, product_keywords) VALUES (NULL,'$product_cat', '$product_brand', '$product_title', '$product_price', '$product_desc', '$product_keyword')";

            if (mysqli_query($con, $sql)) {
                 //you only get the last id AFTER the query runs!
                 $id = mysqli_insert_id();
                 // Place image in the folder 
                 $newname = $id.".jpg";
                 move_uploaded_file( $_FILES['product_image']['tmp_name'], "images/$newname");
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($con);
            }

            $con->close();
    ?>

你的错误就像在真正创建它之前询问服务器创建了什么:)希望你理解我的观点并且它会正常工作

欢呼声

答案 1 :(得分:0)

您需要更改代码的顺序,而不是在获取ID之前执行您的身份。

        $sql = "INSERT INTO products (product_id, product_cat, product_brand, product_title, product_price, product_desc, product_keywords) VALUES (NULL,'$product_cat', '$product_brand', '$product_title', '$product_price', '$product_desc', '$product_keyword')";     
            if (mysqli_query($con, $sql)) {
                echo "New record created successfully";      
                $id = mysqli_insert_id();
                // Place image in the folder 
                $newname = "$id.jpg";
                move_uploaded_file( $_FILES['product_image']['tmp_name'], "images/$newname");
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($con);
            }

            $con->close();