我是PHP和SQL的新手。对于学校作业,我需要为用户创建一个表单来更新客户数据。但是,我注意到更新功能仅使用数据集中的最后一个ID更新客户。例如,如果我在数据集中有4个客户。我使用下拉框列出了客户ID。当我选择id,1~3时,它表示0行被更新。它只适用于我选择id,4。所以我只能更新第4个。有人可以看看我的代码,并给我一些关于问题是什么以及如何解决它的提示?谢谢!
这是“updatecustomer.php”代码:
<?php
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysql(SERVER_NAME, USERNAME,PASSWORD, DATABASE);
if($mysqli->connect_errno){
echo "Connection error " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!($stmt = $mysqli->prepare("UPDATE customer SET fName=?, lName=?, email=?, phone_number=?, address_no=?, address_street1=?,
address_street2=?, address_city=?, address_state=?, address_zip=? WHERE customer_id = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("isssiissssi",$_POST['customer_id'],$_POST['fName'],$_POST['lName'],$_POST['email'],$_POST['phone_number'], $_POST['address_no'],
$_POST['address_street1'],$_POST['address_street2'],$_POST['address_city'],$_POST['address_state'], $_POST['address_zip']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "Updated " . $stmt->affected_rows . " rows to customer.";
}
?>
以下是我表单中代码的一部分:
<div>
<form method="post" action="updatecustomer.php">
<fieldset>
<legend>Update Existing Customer</legend>
<li>Customer ID:
<select name="customer_id">
<?php
if(!($stmt = $mysqli->prepare("SELECT customer_id, customer_id FROM customer"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($customer_id, $customer_id)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo '<option value=" '. $customer_id . ' "> ' . $customer_id . '</option>\n';
}
$stmt->close();
?>
</select>
</li>
<li>First Name: <input type="text" name="fName"> Last Name: <input type="text" name="lName"</li>
<li>Email Address: <input type="text" name="email"></li>
<li>Phone Number: <input type="text" name="phone_number"></li>
<li>Street Number: <input type="text" name="address_no"> Street Line 1: <input type="text" name="address_street1"></li>
<li>Street Line 2 (Apt or Unit Number): <input type="text" name="address_street2"></li>
<li>City: <input type="text" name="address_city"> State: <input type="text" name="address_state"> Zip: <input type="text" name="address_zip"> </li>
</fieldset>
<input type="submit" name="update" value="Update Customer">
<input type="submit" name="delete" value="Delete Customer">
</div>
答案 0 :(得分:0)
检查$stmt->bind_param
行中变量的顺序。
答案 1 :(得分:0)
试试这个:我已经使代码更整洁,更容易理解,并使用bind_param()
方法重新排序了客户ID。
$stmt = $mysqli->prepare("
UPDATE customer
SET fName=?,
lName=?,
email=?,
phone_number=?,
address_no=?,
address_street1=?,
address_street2=?,
address_city=?,
address_state=?,
address_zip=?
WHERE customer_id = ?
");
if(!$stmt){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
$paramBinding = $stmt->bind_param("sssiissssii",
$_POST['fName'],
$_POST['lName'],
$_POST['email'],
$_POST['phone_number'],
$_POST['address_no'],
$_POST['address_street1'],
$_POST['address_street2'],
$_POST['address_city'],
$_POST['address_state'],
$_POST['address_zip'],
$_POST['customer_id']
);
if(!$paramBinding){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
修改强> 然后循环选项:
foreach($stmt->fetch() AS $row){
echo '<option value="'. $row['customer_id'] . '"> ' . $row['customer_id'] . '</option>\n';
}