我正在尝试在单个表单中更新多个列和多个行,但输入的名称是数组,表单如下所示:
<?php
$sql = "SELECT * FROM reservationmenutable where id > 1";
$statement = $conn->prepare($sql);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
$result = $statement->fetchAll();
echo '<table class="table table-striped table-bordered">';
echo "<thead><b>Other services: </b></thead><br>";
echo "<tr>";
echo "<th>Ticket</th>";
echo "<th>Rate</th>";
echo "<th>Toggle Availability // 1 = on, 0 = off </th>";
echo "<th>testheading </th>";
echo "</tr>";
echo "<tbody>";
foreach ($result as $key => $value) {
echo "<tr>";
echo "<td><input type='text' placeholder='{$value['service']}' size='35' name='service_id[{$value['id']}]' ></td>";
echo "<td><input type='number' placeholder='{$value['service_price']}' size='5' name='service_price_id[{$value['id']}]' ></td>";
echo "<td><input type='text' value='{$value['availability']}' name='availability_id[{$value['id']}]'</td>";
echo "<td><input type='text' value='availability_id[{$value['id']}]' name=''
<input type='hidden' value='' name='hidden_id[{$value['id']}]'>
</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
这就是我在更新页面上要做的事情:
<?php
session_start();
include ('../../class/connection.php');
$sql = "UPDATE reservationmenutable SET (service = ?, service_price = ?, availability = ? ) WHERE id = ?";
$arr_value = [
$_POST["service_id[]"],
$_POST["service_price_id[]"],
$_POST["availability_id[]"],
$_POST["hidden_id[]"],
];
$statement = $conn->prepare($sql);
$result = $statement->execute($arr_value);
// $result->rowCount();
if($result){
echo "successfully updated.";
}else{
echo "error.";
}
这是我知道的阵列,但我不知道应该怎么做。
p.s:我还是新手