php - prepared statement within for loop only executes once -


maybe 1 of can jumpstart me here ...

i tried use prepared statement store form values in mysql database php. have $stmt1 , running store information of helpers.

now store additional information e.g. days helpers able help. understanding was, prepare $stmt2 once , use loop find parameter values , execute statement couple times.

preparing statement:

$stmt2 = $dbh->prepare('insert '.$tbl_pre . $tbl_event .'                                 (helper_id,                                 day_date,                                 needcabin)                               values                                 (:helper_id,                                 :day_date,                                 :needcabin)'); $stmt2->bindparam(':helper_id', $helperid); $stmt2->bindparam(':day_date', $day_date); $stmt2->bindparam(':needcabin', $night_id); 

and getting id of last insert statement once, since same coming inserts.

$helperid = $dbh->lastinsertid('id'); 

now starting loop:

for ($i = 0; $i < 3; $i++) {    $insert = false;   $night_id = 0;   $day_date = 0;    if ($night[$i] != null) {      $night_id = 1;     $day_date = $night[$i];     $insert = true;     } elseif ($day[$i] != null){      $day_date = $day[$i];     $night_id = 0;     $insert = true;    }    if ($insert) {      $stmt2->execute();    } } } 

but stops after first iteration. if take execute statement away, runs 3 iterations.

what missing?

your code allows possibility neither case result in insert. is, if neither $day[$i] nor $night[$i] set.

so test see if have loop iterations passing without doing insert:

if ($insert) {     $stmt2->execute(); } else {     echo "no insert executed i=$i"; } 

also, should check both prepare() , execute() returning false, indicates error occurred.

you don't have check if enable pdo exceptions.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -