Hi all I am trying to use a prepared statement to insert an array of users, my array is
Array ( [0] => 1[1] => 4 [2] => 7 ) This works fine, and inserts no problem, but for some reason when it runs the following loop
foreach($users as $user)
{
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$stmt->bindParam(':no', $user, PDO::PARAM_STR);
}
it's using just 1 user record and duplicating it.
So rather than inserting 3 records
1
4
7
it just inserts all 3 records with 7 The full code is
$sql = 'INSERT INTO users (user_id, no) VALUES ';
$i=0;
$count = count($user_array);
foreach($users as $user)
{
$i++;
$sql .= "(:user_id,:no)";
if($i < $count)
{
$sql .= ",";
}
}
$stmt = $conn->prepare($sql);
foreach($users as $user)
{
$stmt->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$stmt->bindParam(':no', $user, PDO::PARAM_STR);
}
$stmt->execute();
any ideas? Is it because the bindParam's need a unqiue name in their loop?
Thanks