1

我对 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>
4

2 回答 2

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';
}
于 2013-06-04T16:37:06.410 回答
0

检查行中变量的顺序$stmt->bind_param

于 2013-06-04T16:31:42.207 回答