【问题标题】:PHP - blank page after executing mysqli INSERT query with data from HTML formPHP - 使用 HTML 表单中的数据执行 mysqli INSERT 查询后的空白页
【发布时间】:2013-11-30 20:37:01
【问题描述】:

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

我完全不知所措。在过去的几个小时里,我一直在为此苦苦挣扎。我查看了很多类似的问题,但我仍然没有看到/理解我的代码出了什么问题。注意:$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 手动添加条目,那么我可以查询数据库并在我的网页上回显一个列表。对于冗长的代码,我深表歉意,但我确实没有想法,我希望有人能阅读并提供帮助。

【问题讨论】:

  • 首先:error_reporting(-1);
  • 白屏死机表示服务器错误。查看您的服务器错误日志以找出问题所在。日志通常位于 Linux 机器上的 /etc/httpd/logs 中。

标签: php database post insert mysqli


【解决方案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);

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

【讨论】:

  • 我添加了error_reporting(-1);我也再次检查了我的括号,但仍然没有运气编辑:对不起,我第一次没有正确放置右括号谢谢你,谢谢你现在可以使用了!我不敢相信我在括号上浪费了这么多时间......
  • 空白页通常意味着错误。另一件事是你能看到错误。我建议您在 php.ini 中打开完整的错误报告并重新启动服务器(error_reporting = E_ALL 及以下 display_errors = On)
【解决方案2】:

这是做什么的?

header("location: inventory_list.php");

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

【讨论】:

  • 添加条目后应该会再次将我带到同一页面。这并没有造成任何问题。感谢您抽出宝贵的时间来回答 - 这只是一个缺少的括号:)
猜你喜欢
  • 2016-12-30
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
相关资源
最近更新 更多