简体   繁体   中英

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? Every time I select "ID #3" in the drop down box of a form, my update query always reads 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:

    <?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
Try with a double enquoted string like this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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