php - PDO update only updating one row -
so quite confused problem. sending post php file using ajax , takes array, reverses it, chunks , uses bind values. each chunk holds row worth of info update, each iteration of loop updates row.
the first time attempted used following code.
if(isset($_post['saveedits'])) { $i = 0; $j = 1; $update = array(); $update = $_post['saveedits']; $chunk_count = count($update)/7; $backwards = array_reverse($update); $chunks = array_chunk($backwards, 7); try { for($i; $i < $chunk_count; $i++ ) { $update_project = $db->prepare(' update projects set comments = ?, contact = ?, est_end = ?, est_start = ?, apm = ?, pm = ? id = ? '); foreach ($chunks[$i] $field => $val) { $update_project->bindvalue($j++, $val, pdo::param_str); } $update_project->execute(); } echo 'projects updated'; } catch(pdoexception $e) { die($e->getmessage()); } } else { echo 'could not update projects table'; } and following error each time
warning: pdostatement::execute(): sqlstate[hy093]: invalid parameter number: parameter not defined in c:\xampp\htdocs\core\functions\update_projects.php on line 31 so suggested change
$i = 0; to
$i = 1; this somehow works. lets me insert data 1 time, not insert of data. fact works at all baffling me. $i holder loop index, , chunks index. needs go 0 - whatever. don't understand how starting @ 1 helps in way. $j holder param binder, should start @ 1.
some additional info. if var_dump(chunks[0]) , var_dump(chunks[1]) see following
array(7) { [0]=> string(13) "more comments" [1]=> string(9) "jimmy doe" [2]=> string(6) "1-1-14" [3]=> string(7) "12-1-13" [4]=> string(9) "janey doe" [5]=> string(9) "jonny doe" [6]=> string(2) "17" } 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 review. $i = 0 , error top, $i = 1 insert 1 row , it's over. can please tell me what's going on here? appreciated
there multiple problems code. main issue though way attempting split array chunks. let's have 10 items in array, given code $chunk_count = count($update)/7;, give chunk count of 1.4285..., greater 1, less 2. in loop, expect run once if $i = 1; because first time through loop, 1 < 1.x, second time 2 > 1.x.
what trying accomplish breaking array? process straight through. remember keep simple when possible.
Comments
Post a Comment