如何更新所选行的数据?

时间:2013-06-04 20:05:57

标签: php sql database mysqli sql-update

我正在学习PHP,HTML和SQL。我正在尝试创建一个允许用户更新现有客户数据的表单。我想知道我的代码出了什么问题,因为每当我尝试对现有客户进行更改时,更改都会反映在数据集的第一个客户身上。例如,当我更改客户ID#14的数据时,更改显示在客户ID#1中。客户ID#14仍保留旧数据。这是我的代码。有人可以给我一些如何解决它的提示吗?我花了好几个小时,但我不知道我错过了什么。非常感谢!

这是main.php文件中代码的一部分。

<div>
            <form method="post" action="updatecustomer.php">
                <fieldset>
                    <legend>Update Existing Customer</legend>
                    <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>
                    <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>
                </fieldset>
                <input type="submit" name="update" value="Update Customer">
        </div>

这是updatecustomer.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.";
}

1 个答案:

答案 0 :(得分:0)

不确定,但请先尝试在此处删除空格value=" '. $customer_id . ' ",因为这会向您发送类似string(4) " 14 "的字符串类型值。我接下来要intval($_POST['customer_id'])