0

有人可以告诉我如何将 ID 传递给 PHP/MySQL 中的 UPDATE 查询吗?每次我在表单的下拉框中选择“ID #3”时,我的更新查询总是读取 ID #1。我相信问题出在下面的代码中。我已经坚持了2天。我无法说出我的代码中缺少什么。非常感谢您的帮助。

        <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 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)){
        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
//Turn on error reporting
ini_set('display_errors', 'On');

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;
}

echo $_POST['customer_id'];

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.";

}


$stmt->close();


?>
4

1 回答 1

0

此行的第一部分是单引号:

 echo '<option value=".$customer_id."> '.$customer_id.'</option>\n';

这意味着该值的变量“$customer_id”将不会按照文档
扩展 尝试使用这样的双引号字符串:

echo "<option value=\"$customer_id\" >$customer_id</option>\n";
于 2013-06-06T15:21:08.927 回答