PHP - 使用HTML表单中的数据执行mysqli INSERT查询后的空白页

时间:2013-11-30 20:37:01

标签: php database post insert mysqli

我是PHP的新手。我正在努力将数据添加到MySQL数据库表'产品'中,从输入到HTML表单的数据(产品名称,价格,类别等)中我先写了一些确实有用的东西,但它给了我一个警告。我在代码中更改了一些内容以更正警告(我不记得我改变了什么),现在当我点击表单上的提交按钮时,我得到的只是一个空白页。

我完全失去了。在过去的几个小时里,我一直在努力解决这个问题。我看了很多类似的问题,但我仍然没有看到/理解我的代码中出了什么问题。注意:$con是数据库连接,已经建立。

<?php 

// Parse the form data and put it into variables to add into the system
if (isset($_POST['product_name'])) {

  $product_name = mysqli_real_escape_string($con,$_POST['product_name']);
    $price = mysqli_real_escape_string($con,$_POST['price']);
    $category = mysqli_real_escape_string($con,$_POST['category']);
    $subcategory = mysqli_real_escape_string($con,$_POST['subcategory']);
    $details = mysqli_real_escape_string($con,$_POST['details']);

  // See if the product name that you're trying to add is a match to another existing product
  // If it is a match, then echo out that you're trying to add a duplicate product and exit
    $sql = mysqli_query($con,"SELECT id FROM products WHERE product_name='$product_name' LIMIT 1");
    $productMatch = mysqli_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'It appears that a product by that name already exists. To go back, <a href="https://project.cs.cf.ac.uk/V.Bhatia/coursework/storeadmin/inventory_list.php">click here.</a>';
        exit();
    }

  // If the product match check showed that the product doesn't exist in the database, add the product
    $sql = mysqli_query($con,"INSERT INTO products (product_name, price, details, category, subcategory) 
        VALUES('$product_name','$price','$details','$category','$subcategory'") or die (mysql_error());

  // This is the picture ID. Putting it in a variable "pid". Pictures are connected to products by their ID. A product of id "1" will have a picture "1.jpg"
  $pid = mysql_insert_id();

  // Place image in the folder
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], "https://project.cs.cf.ac.uk/V.Bhatia/coursework/images/$newname");

  // Come back to the inventory_list page after the adding process is done and exit the script
  header("location: inventory_list.php"); 
    exit();
}
?>

这是HTML表单:

<h3>&darr; Add New Inventory Item Form &darr;</h3>
    <form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
    <table width="90%" border="0" cellspacing="0" cellpadding="6">
      <tr>
        <td width="20%" align="right">Product Name</td>
        <td width="80%"><label>
          <input name="product_name" type="text" id="product_name" size="64" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Price</td>
        <td><label>
          $
          <input name="price" type="text" id="price" size="12" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Category</td>
        <td><label>
          <select name="category" id="category">
          <option value="Clothing">Clothing</option>
          </select>
        </label></td>
      </tr>
      <tr>
        <td align="right">Subcategory</td>
        <td><select name="subcategory" id="subcategory">
        <option value=""></option>
          <option value="Hats">Hats</option>
          <option value="Pants">Pants</option>
          <option value="Shirts">Shirts</option>
          </select></td>
      </tr>
      <tr>
        <td align="right">Product Details</td>
        <td><label>
          <textarea name="details" id="details" cols="64" rows="5"></textarea>
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Image</td>
        <td><label>
          <input type="file" name="fileField" id="fileField" />
        </label></td>
      </tr>      
      <tr>
        <td>&nbsp;</td>
        <td><label>
          <input type="submit" name="button" id="button" value="Add This Item Now" />
        </label></td>
      </tr>
    </table>
    </form>

否则,数据库连接正常。如果我使用phpMyAdmin手动添加条目,那么我就可以查询数据库并在我的网页上回显一个列表。我为冗长的代码道歉,但我完全没有想法,我希望有人可以阅读并帮助。

2 个答案:

答案 0 :(得分:1)

// If the product match check showed that the product doesn't exist in the database, add the product
  $sql = mysqli_query($con,"INSERT INTO products (product_name, price, details, category, subcategory) 
      VALUES('$product_name','$price','$details','$category','$subcategory'") or die (mysql_error());

你错过了SQL代码中的右括号')'。

error_reporting(-1);

在代码的开头应该有帮助。

答案 1 :(得分:0)

这应该做什么?

header("location: inventory_list.php");

您是否确定错误发生之前或者如果您没有传递任何变量,您的inventory_list.php是否可能出现空白?