我做了一个页面,使用mysqli和php将清单添加到数据库。当我填写信息并单击“提交”按钮时,没有任何反应。字段变为空白。我检查了数据库,但没有放入任何内容。
有人可以帮我弄清楚错误在哪里吗?
这是我的代码:
<?php
error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
include_once("../storescripts/connect_to_mysql.php");
$con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
if(isset($_POST['button'])) {
$item_number = mysqli_real_escape_string($con, $_POST['item_number']);
$price = mysqli_real_escape_string($con, $_POST['price']);
$category = mysqli_real_escape_string($con, $_POST['category']);
$desc = mysqli_real_escape_string($con, $_POST['description']);
$qty = mysqli_real_escape_string($con, $_POST['qty']);
// See if that product name is an identical match to another product in the system
$sql = ("SELECT id FROM products WHERE item_number='$item_number' LIMIT 1");
$result = mysqli_query($con, $sql);
$productMatch = mysqli_num_rows($result); // count the output amount
if ($productMatch > 0) {
echo 'Sorry you tried to place a duplicate "item_number" into the system, <a href="inventory_list.php">click here</a>';
exit();
}
//Add to inventory now
$sql = "INSERT INTO products ('item_number', 'price', 'category', 'description', 'qty', 'date_added')
VALUES (?,?,?,?,?,?)";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt, $sql)) {
echo "sql error";
} else {
mysqli_stmt_bind_param($stmt, "ssssss", $item_number, $desc, $price, $catagory, $qty, now());
mysqli_stmt_execute($stmt);
echo mysqli_stmt_error($stmt);
$pid = mysqli_insert_id();
// Place image in the folder
$newname = "$pid.png";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../pictures/inventory/$newname");
}
}
?>
<?php
$product_list = '';
include_once("../storescripts/connect_to_mysql.php");
$con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
$sql = "SELECT * FROM products ORDER BY date_added DESC";
$result = mysqli_query($con, $sql);
$product_count = mysqli_num_rows( $result);
if ($product_count > 0) {
while($row = mysqli_fetch_array($sql)){
$id = $row["id"];
$item_number = $row["item_number"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
$qty = $row["qty"];
}
} else {
$product_list = "You Have No Items In Inventory";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="samuel jaycox">
<title>Inventory List</title>
<link rel="stylesheet" href="../style.css" type="text/css" media="screen" />
<link rel="shortcut icon" type="image/png" href="pictures/pinky.png">
</head>
<body>
<div align="center" id="mainWrapper">
<div id="pageContent"><br />
<div align="right" style="margin-right:32px;"><a href="inventory_list.php#inventoryForm">+ Add New Inventory Item </a></div>
<div align="left" style="margin-left:24px;">
<h2>Inventory list</h2>
<?php echo $product_list; ?>
</div>
<hr />
<a name="inventoryForm" id="inventoryForm"></a>
<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">Item Number</td>
<td width="80%"><label>
<input name="item_number" type="text" id="item_number" 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="Bracelets">Bracelet</option>
<option value="Necklace">Necklace</option>
<option value="Earring">Earring</option>
<option value="Childrens">Childrens</option>
<option value="Sets">Sets</option>
<option value="Rosary">Rosary</option>
<option value="Accessories">Accessories</option>
</select>
</label>
</tr>
<tr>
<td align="right">Description</td>
<td><label>
<textarea name="description" id="description" cols="64" rows="20"></textarea>
</label></td>
</tr>
<tr>
<td width="20%" align="right">Qty</td>
<td width="80%"><label>
<input name="qty" type="text" id="qty" size="9" />
</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="Submit"/>
</label></td>
</tr>
</table>
</form>
<br />
<br />
</div>
</div>
</body>
</html>
我是mysqli的新手,只是不知道从哪里开始寻找。 任何帮助,将不胜感激。谢谢
答案 0 :(得分:0)
将以下代码放在php代码的顶部:
error_reporting(E_ALL);
ini_set('display_startup_errors', 1); // add this too
ini_set('display_errors', 1);
它将显示所有对您有帮助的错误,也请在mysql_stmt_execute()
之后添加此代码:
echo mysqli_stmt_error($stmt);
此代码将返回可能会帮助您的mysql错误消息。不要忘记将错误消息粘贴到您的问题。
编辑:关于您的代码,
如果我是正确的,那么您正在尝试使用准备好的语句。应该是
//add ? marker, see official php page about prepared statements
$sql = "INSERT INTO products ('item_number', 'description', 'price',
'catagory', 'qty', 'date_added')
VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt, $sql)) {
echo "sql error";
} else {
//parameters (variable) are binded here. s for string, see official php page for more information.
mysqli_stmt_bind_param($stmt, "ssssss", $item_number, $desc, $price, $catagory, $qty, now());
mysqli_stmt_execute($stmt);
$pid = mysqli_insert_id();
// Place image in the folder
$newname = "$pid.png";
move_uploaded_file( $_FILES['fileField']['tmp_name'],
"../pictures/inventory/$newname");
}
header("location: inventory_list.php?item addes successfully");
}
编辑2:
尝试在$con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
之后添加此代码以检查您的连接。
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
还要从$sql = "INSERT INTO products ('item_number', 'price', 'category', 'description', 'qty', 'date_added') VALUES (?,?,?,?,?,?)";
中删除'(撇号)
编辑3:
php中没有now()
函数,您可以将其更改为date("Y-m-d H:i:s")
或将准备好的语句代码更改为以下代码。
//now() function is mysql function, so it should be called in sql query.
$sql = "INSERT INTO products ('item_number', 'description', 'price',
'catagory', 'qty', 'date_added')
VALUES (?, ?, ?, ?, ?, NOW())";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt, $sql)) {
echo "sql error";
} else {
//remove now() function.
mysqli_stmt_bind_param($stmt, "sssss", $item_number, $desc, $price, $catagory, $qty);
mysqli_stmt_execute($stmt);
$pid = mysqli_insert_id();
// Place image in the folder
$newname = "$pid.png";
move_uploaded_file( $_FILES['fileField']['tmp_name'],
"../pictures/inventory/$newname");
}
答案 1 :(得分:0)
您的查询值部分是错误的。应该是Values(“ value1”,依此类推) 在您的情况下,仅仅是值(value1,那不是正确的方法。
您始终可以回显Sql语句,并在Sql客户端(例如HeidiSQL或MySQL的CLI)中运行查询。