Been redoing the site. Upgraded to mysqli and now I'm tidying up the code and securty with prepared statements. I understand that statements should be prepared outside of foreach loops but I'm wondering about conditional statements.
[code that decides $table]
foreach ($_POST[$lastvar] as $key => $value) {
[code not relevant to Q]
$sql3 = "SELECT * from $table WHERE titlesid=? and peopleid=?";
$stmt3 = $mysqli->prepare($sql3);
$stmt3->bind_param("ii", $titlesid,$peopleid);
$stmt3->execute();
if ($stmt3->num_rows == 0) {
if ($table == "dhereviewers") {
$sql = "INSERT into $table (titlesid,peopleid) VALUES (?,?)";
} else {
$sql = "INSERT into $table (titlesid,peopleid,billing) VALUES (?,?,?)";
}
$billing++;
[prepare/execute one of the last two statements]
}
}
}
So depending on the 'if' I'm going to execute one or the other of the last two inserts. Because they are conditional, do I only prepare them if they're "chosen"?
Hope I'm clear. :-)
Still learning the ropes of prepared statements.