简体   繁体   中英

PDO Prepared Statements: having trouble binding multiple values to query

I am new to PDO and prepared statements and I am having trouble binding multiple values to my query. I have no problem if it is just one while making a SELECT, for example:

SELECT foo FROM table WHERE id=:something // no problem

But multiple, and trying to INSERT I am getting stuck:

Insert INTO mytable (field1, field2) VALUES (:value1, :value2) // No bueno 

Have tried a few different ways and read other posts on here but no luck. Below is an example of what I am having trouble with:

$insertSQL = $db->prepare("INSERT INTO voting_poll (ipaddress, choice) 
    VALUES (':ipaddress', :value)");
    $insertSQL->bindParam(':ipaddress', getenv('REMOTE_ADDR'), PDO::PARAM_STR);
    $insertSQL->bindParam(':value', $_POST['radio'], PDO::PARAM_STR);
    $insertSQL->execute();

I am getting the following error: Invalid parameter number: number of bound variables does not match number of tokens

  1. You don't put quotes around params. Remove the quotes around :ipaddress in your query.

  2. bindParam() must be used with a variable as it binds the parameter to the variable reference. If you want to use a value (for example, the return value from a function like getenv() ), use bindValue() instead.

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