multiple PDO insert with single statement not working

jon

I am trying to loop through $vars_array['selected_ids'] array and insert the selected_id and page_id. It is inserting 3 records (as expected), but each record is getting a selected_id of 7.. i.e. the last selected value inserted.

$vars_array=array();
$vars_array['page_id']=10;
$vars_array['selected_ids']=array(2,3,7);

$sql = "INSERT INTO template_values ( page_id, selected_id) VALUES";
$sqlPart = array_fill(0, count($vars_array['selected']), "(?, ?)");
$sql .=  implode(",",$sqlPart);
$stmt=$this->database->prepare($sql);

$i = 1;
foreach($vars_array['selected_ids'] as $selected_id) {
    $stmt -> bindParam($i++, $vars_array['page_id']);
    $stmt -> bindParam($i++, $selected_id);
}
$stmt -> execute();

Any ideas? regards J

MatsLindh

The reason is because you're using bindParam, and not bindValue. bindParam stores a reference to the variable you're binding, and uses the value of the variable at time of execution. That value is the same as the last iteration in your foreach.

Either use bindValue (which you usually should do anyway, since you're expecting it to use the value and not a reference - leading to subtle bugs like this) or send in an array to execute directly (which I usually prefer when generating a large placeholder query, since I prefer named placeholders when using bindValue).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

INSERT not working with PDO prepared statement

From Dev

PDO MYSQL insert statement not working (no error given)

From Dev

Insert multiple rows of data in a single SQL statement

From Dev

Creating a single insert statement for multiple array values

From Dev

Insert multiple rows of data in a single SQL statement

From Dev

Insert and Update multiple values in single SQL Statement

From Dev

PDO large insert statement

From Dev

Converting multiple SQL Insert statements into one single Insert statement

From Dev

Converting multiple SQL Insert statements into one single Insert statement

From Dev

PDO INSERT INTO not working?

From Dev

PDO/SQL insert not working

From Dev

INSERT statement does not work with PDO

From Dev

Sql Insert Into Select statement PDO

From Dev

Make a single insert statement

From Dev

PDO update prepared statement not working

From Dev

INSERT statement not working mysql

From Dev

PDO fetch single class not working

From Dev

PDO fetch single class not working

From Dev

PHP PDO Multiple Prepare Statement

From Dev

PDO insert not inserting with multiple values

From Dev

PHP PDO Multiple image INSERT

From Dev

Insert post with multiple tags in PDO

From Dev

Pdo multiple row insert issue

From Dev

Multiple recursions in a single statement

From Dev

pdo insert statement using datetime errors

From Dev

PDO Prepared statement insert 1 instead of string

From Dev

PDO insert statement with loop through $_POST array

From Dev

How to include SELECT statement into INSERT INTO with concatenation & pdo

From Dev

PHP PDO insert statement not going through