简体   繁体   English

在 foreach 循环内自动增加会话索引

[英]Auto increment session index inside foreach loop

I have a foreach loop that displays database rows dynamically.我有一个动态显示数据库行的 foreach 循环。 I'm displaying them inside a form so when I click the button inside one of the forms that got displayed I go to another page which updates my database based on what form I selected.我将它们显示在表单中,因此当我单击显示的其中一个表单中的按钮时,我会转到另一个页面,该页面根据我选择的表单更新我的数据库。

My only idea about how I can grab the specific row and update it is by setting a session for each form created;我关于如何获取特定行并更新它的唯一想法是为每个创建的表单设置一个会话; how can I set a session with an auto incrementing index?如何设置具有自动递增索引的会话?

<?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;?> 

If I leave the session like this, it gets overwritten.如果我像这样离开会话,它会被覆盖。

Update.php更新.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']);

If $row matches to a row in a database table, it should have a unique id.如果 $row 匹配数据库表中的一行,它应该有一个唯一的 id。 You can pass that unique id in a hidden input in the form:您可以在表单的隐藏输入中传递该唯一 ID:

<input type = "hidden" name = "id" value = "<?= htmlspecialchars($row['id']) ?>" />

Then in Update.php , reference $_POST['id'] rather than $_SESSION['id']然后在Update.php ,引用$_POST['id']而不是$_SESSION['id']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM