В этой версии используются как подготовленные операторы, так и построение одного оператора SQL.Я добавил код в комментарии, чтобы помочь ...
// Values used to bind to prepare
$binds = [];
// The types of the binds
$types = "";
// The SQL statement
$update = "update myTable set ";
for($i=1; $i<=3; $i++){
// If this input is set
if ( isset($_POST['input'.$i])) {
// Store it in the array
$binds[] = $_POST['input'.$i];
// Add in part of SQL - e.g. nh1 = ?,
$update .= "nh$i = ?,";
// Add type of parameter (string)
$types .= "s";
}
}
// Add where clause (remove trialing comma from list of fields to update
$update = substr ($update, 0, -1)." where id=1";
$stmt = $mysqli->prepare($update);
// Bind the variables
$stmt->bind_param($types, ...$binds );
$stmt->execute();
Утверждение, которое он строит, будет выглядеть примерно так ...
update myTable set nh1 = ?,nh3 = ? where id=1
с данными для привязки, показанными ниже,поэтому каждый из этих элементов будет помещен вместо соответствующего заполнителя (?
) в SQL.
Array
(
[0] => value for input 1
[1] => value for input 3
)