I would like to update a SQL table using PHP with PDO. However I keep getting the following error
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in C:\xampp\htdocs\core\functions\update_projects.php on line 31
I just can't make sense of where I'm going wrong.
$j = 1;
$chunk_count = count($update)/7;
$backwards = array_reverse($update);
$chunks = array_chunk($backwards, 7);
var_dump($chunks[1]);
try {
for($i = 0; $i < $chunk_count; $i++ ) {
$update_project = $db->prepare('
UPDATE projects
SET comments = ?,
contact = ?,
est_end = ?,
est_start = ?,
apm = ?,
pm = ?
WHERE id = ?
');
foreach ($chunks[$i] as $field => $val) {
$update_project->bindValue($j++, $val, PDO::PARAM_STR);
}
$update_project->execute();
}
echo 'Projects Updated';
} catch(PDOException $e) {
die($e->getMessage());
}
If I var_dump($chunks[1]) I see the following values
array(7) { [0]=> string(13) "some comments" [1]=> string(7) "jim doe" [2]=> string(6) "1-1-14" [3]=> string(7) "12-1-13" [4]=> string(8) "jane doe" [5]=> string(7) "jon doe" [6]=> string(2) "16" }
So where is the problem in my code? Any help is appreciated
i = 0toi = 1it works, but I'm curious as to why. Shouldn't$chunksstart at a 0 index?