0

我是 php/mysqli 的初学者。我一直在尝试创建一个允许用户更新客户数据的表单。我相信我的 php 代码是正确的,但是每次我对客户进行更改时,所有新数据都会显示在数据集中第一个客户的记录中。你能看看我下面的代码,给我一些关于我缺少什么的指导吗?谢谢!

这是 main.php 文件的一部分

<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">
        </div>

这是updatecustomer.php

    <?php

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("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']))){
    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.";
    echo $_POST['customer_id'];

}


$stmt->close();


?>
4

1 回答 1

-1

我建议您将自己定义的变量绑定为参数,而不是实际的 $_POST 数据。

if($stmt->bind_param("sssiissssii",$fname,$lname$email, ... ,$customerid))
{
 $fname = $_POST['fname'];
 $lname = $_POST['lname'];
 $email = $_POST['email'];
 ...
 $customerid = $_POST['customer_id'];
} 
else
{
  //error message here then exit
} 

它与 bind_param 方法的内部有关。请试一试,然后告诉我进展如何。

此外,您在 updatecustomer.php 中进行错误处理的测试方式可能需要进行一些更改。如果准备语句失败,则它已经给定之后的所有内容都将失败。随后的方法调用也是如此。如果它们之前的方法失败,它们将失败,如果您在打印错误消息后 exit() 可能会更好,以减少代码造成的混乱。

于 2013-06-06T05:43:27.353 回答