我有一个动态显示数据库行的foreach循环。我将它们显示在一个表单中,所以当我点击其中一个表单中的按钮时,我将转到另一个页面,该页面根据我选择的表单更新我的数据库。
我唯一的想法是如何抓住特定的行和更新它是通过为每个表单创建一个会话;如何将会话设置为自动递增索引?
<?php foreach($stmt as $row):?>
<form action="Update.php" method="POST" >
<p>Has <?php echo $row["info"]; ?> seat(s) available </p>
//other similar html elements and echoing from database
<button type="submit" name="updateData"> Reserve</button>
// $_SESSION['phone']=$row['phone']
</form>
<?php endforeach;?>
如果我这样离开会话,它会被覆盖。
Update.php
$sss=5;
if(isset($_POST['updateData'])){
$stmt = $db->prepare("UPDATE ddd SET seats =:seats where phone = :phone");
$stmt->bindParam(':seats', $sss);
$stmt->bindParam(':phone', $_SESSION['phone']);
如果$row匹配数据库表中的一行,它应该有一个唯一的id。你可以在一个隐藏的输入中传递这个唯一的id:
<input type = "hidden" name = "id" value = "<?= htmlspecialchars($row['id']) ?>" />
则在Update.php
中引用$_POST['id']
而不是$_SESSION['id']