只能将表的最后一行数据插入到会话

时间:2016-07-31 18:06:09

标签: php html mysql

这是我的代码

                <?php
                    $count = 1;
                    $sql = "SELECT * FROM `scheduledata`
                    WHERE `departdate` = '$departdatephp'
                    AND   `orginplace_id` = '$orginplacephp'
                    AND   `desplace_id`   = '$desplacephp' ";
                    $result = mysqli_query($conn, $sql);
                    while($row = mysqli_fetch_assoc($result))
                    {
                         ?>


                        <tr>
                            <th><?php echo $count;?></th>
                            <th><?php echo $row["departtime"];?><input type="hidden" name="departtime" value="<?php echo $row['departtime'];?>" method="post"></th>
                            <th><?php echo $row["returntime"];?><input type="hidden" name="returntime" value="<?php echo $row['returntime'];?>" method="post"></th>
                            <th><?php echo $row["adultprice"]; ?>MYR<input type="hidden" name="adultprice" value="<?php echo $row['adultprice'];?>" method="post"></th>
                            <th><?php echo $row['schedule_id'];?><input type="hidden" name="scheduleid" value="<?php echo $row['schedule_id'];?>" method="post"></th>
                            <th><?php echo $row['flight_id'];?><input type="hidden" name="flightid" value="<?php echo $row['flight_id'];?>" method="post"></th>
                            <th><button type="submit" class="submit-button" method="post" name="departbtn[]">booking</button></th>
                            <?php
                                $count = $count+1;}
                             ?>
                        </tr>

Lest说我有三种这样的预订选择 3 booking button

这是我的输出代码

<?php
  if(isset($_POST['departbtn']))
  {
$departtimephp = $_POST['departtime'];
$returntimephp   = $_POST['returntime'];
$price = $_POST['adultprice'];
$scheduleid   = $_POST['scheduleid'];
$flightid   = $_POST['flightid'];

$_SESSION['departtime'] = $departtimephp ;
$_SESSION['returntime'] = $returntimephp ;
$_SESSION['adultprice']   =   $price ;
$_SESSION['scheduleid']   = $scheduleid ;
$_SESSION['flightid']= $flightid  ;

print_r($_SESSION['departtime']);
print_r($_SESSION['returntime']);
print_r($_SESSION['adultprice']);
print_r($_SESSION['scheduleid']);
print_r($_SESSION['flightid']);
 }
  ?>

无论我选择哪个选项,输出总是表的最后一行,我猜它因为所有都有相同的变量名,所以系统直接插入最后一行数据。 我的问题有点像here,他们建议使用array []来解决这个问题,但是当我在isset中添加foreach时出现错误,所以任何想法如何获取正确的数据? 感谢任何解决方案。

1 个答案:

答案 0 :(得分:0)

显示的代码并未明确显示,但我强烈怀疑整个表格是您的form。这意味着无论您点击哪个按钮,都会提交所有值。

对于初学者来说,浏览器不会知道你指的是哪一个&#34;并且任何给定的值都只会覆盖POST形式的前一个值。 (这解释了你所看到的行为......你总是得到最后的记录。)但是,除此之外......为什么?当您真正需要所选记录的单个标识符时,为什么要提交所有数据

对于表格中的每一行,请放置一个完整的自包含form。像这样:

<tr>
    <!--- your other table cells --->
    <th>
        <form method="post" action="someAction.php">
            <input type="hidden" name="flightid" value="<?php echo $row['flight_id'];?>" />
            <button type="submit" class="submit-button" method="post" name="departbtn">booking</button>
        </form>
    </th>
</tr>

(您可以在那里放置更多hidden表单字段,尽可能多地使用。虽然老实说您应该只需要一个标识符。服务器已经拥有其余的数据。而且您拥有的字段越多使用只是让用户弄乱系统的更多机会。)

当你点击一个按钮时,选择的选项没有歧义。每个表单都是完全独立的,只有它需要发布的数据。