【发布时间】: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>↓ Add New Inventory Item Form ↓</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> </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