简体   繁体   English

如何将id变量传递给php / mysqli中的查询?

[英]how to pass a id variable to a query in php/mysqli?

Can someone tell me how I can pass an ID to an UPDATE query in PHP/MySQL? 有人可以告诉我如何将ID传递给PHP / MySQL中的UPDATE查询吗? Every time I select "ID #3" in the drop down box of a form, my update query always reads ID #1. 每当我在表单的下拉框中选择“ ID#3”时,我的更新查询总是读取ID#1。 I believe the problem is somewhere in the code below. 我相信问题出在下面的代码中。 I've been stuck with this for 2 days. 我已经坚持了两天。 I can't tell what I'm missing in my code. 我无法告诉我代码中缺少的内容。 Your help is greatly appreciated. 非常感谢您的帮助。

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

Here is the updatecustomer.php file: 这是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();


?>

The first part of this line is single-quoted : 该行的第一部分用单引号引起来:

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

That means that the variable "$customer_id" for the value will not be expanded as per the doc 这意味着该值的变量“ $ customer_id”不会按照文档扩展
Try with a double enquoted string like this : 尝试使用双引号这样的字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM