dopa53272 2018-08-03 14:14
浏览 80
已采纳

PHP / MySQL - 按钮点击时不会获得表格中的第一个ID

We are creating an HTML table where if a user selects a "Checkout" button next to a name, it will update a row in the database with the current date and time while removing them from the HTML table. It does this by grabbing ID of the row selected. For some reason, when there are more than one names in the HTML table, if you click the Checkout button of the first one, it grabs the ID of the name below it and updates that row in the database. If you click the Checkout button of the one below the first one, then it does executes the code as normal.

Page code:

    <?php
    // connect to database
    require_once('connection.php');
    session_start();

    //get session variable, if empty, unset and logout
    if(empty($_SESSION['department'])) {
    session_unset();
    session_destroy();
    header("Location: index.php");
    } else {
    }

    $dept = $_SESSION[department];
    $eventsTable = $dept . "_events";
    $checkinTable = $dept . "_checkin";

    $query = mysqli_query($VisitorManagement, "SELECT * FROM config WHERE deptCode='$dept'");
    $row2 = mysqli_fetch_array($query);
    $checkout = $row2['checkout'];

    $sql = mysqli_query($VisitorManagement, "SELECT * FROM {$checkinTable} WHERE deptCode='$dept'");
    $row3 = mysqli_fetch_array($sql);
    $checkoutDateTime = $row3['checkoutDateTime'];

    if (!$checkout) {
    header("Location: checkin.php");
    }

    if(isset($_POST['checkoutUser']))
    {
    $id = mysqli_real_escape_string($VisitorManagement, $_POST['id']);
    // set checkout date
    $query = "UPDATE {$checkinTable} SET checkoutDateTime = CURRENT_TIMESTAMP() WHERE id = '$id'";
    if (mysqli_query($VisitorManagement, $query)) {
        header('Location: checkout-thankyou.php');
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($VisitorManagement);
    }
    }


    ?>

    <!doctype html>
    <html class="no-js" lang="en">
    <head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Visitor Management</title>
    <link rel="stylesheet" href="css/foundation.min.css" />
    <link rel="stylesheet" href="css/app.css" />
    </head>
    <body>
    <!-- nav -->
    <div class="top-bar">
    <div class="top-bar-left">
    <ul class="menu">
    <li><a href="checkin.php">Check-In</a></li>
    <?php 
        if ($checkout) {
    ?>
    <li><a href="checkout.php" class="active">Checkout</a></li>
    <?php } ?>
    </ul>
    </div>
    <div class="top-bar-right">
    <ul class="menu">
      <li><a href="login.php">Admin Login</a></li>
      <li><a href="logout.php">Logout</a></li>
    </ul>
    </div>
    </div>
    <div class="row text-center" style="margin-top: 5%;">
        <h1><img src="img/su-wordmark-vm.png" alt="Syracuse University"></h1>
        <?php
    $sql = "SELECT name from departments WHERE code='{$dept}'";
    $result = mysqli_query($VisitorManagement, $sql);
    $row = mysqli_fetch_array($result);
    $name = $row["name"];
    echo "<h2> $name </h2>"
    ?>
      </div>


       <div class="row">
    <div class="medium-12 columns text-center" style="padding-top: 5%;">
      <p>Please select the checkout button of the visitor you wish to checkout.</p>

      <form id='checkout' method='post' name='checkout'>
       <?php
                 $checkinTable = $dept . "_checkin";

                $result = mysqli_query($VisitorManagement, "SELECT * FROM {$checkinTable} WHERE DATE(checkinDateTime) = CURDATE() AND checkoutDateTime IS NULL ORDER BY checkinDateTime DESC");

                echo "<table class='checkin'>
                <thead>
                <tr>
                <th>Student Name</th>
                <th>SUID #</th>
                <th>Student Email</th>
                <th>Primary Affiliation</th>
                <th>Program</th>
                <th>Education Level</th>
                <th>Staff Member</th>
                <th>Reason for Visit</th>
                <th>Check In Date/Time</th>
                <th>Checkout</th>
                <th aria-hidden='true' class='hide'>Hidden User ID</th>
                </tr>
                </thead>";


                    while($row = mysqli_fetch_array($result))
                    {
                        $phpdate = strtotime($row['checkinDateTime']);
                        $dateTime = date('F j, Y, g:i a', $phpdate);
                                echo "<tbody>";
                                echo "<tr>";
                                echo "<td>" . $row['studentName'] . "</td>";
                                echo "<td>" . $row['suid'] . "</td>";
                                echo "<td>" . $row['studentEmail'] . "</td>";
                                echo "<td>" . $row['studentAffiliation'] . "</td>";
                                echo "<td>" . $row['studentProgram'] . "</td>";
                                echo "<td>" . $row['studentEduLevel'] . "</td>";
                                echo "<td>" . $row['staffMember'] . "</td>";
                                echo "<td>" . $row['reasonForVisit'] . "</td>";
                                echo "<td>" . $dateTime . "</td>";
                                echo "<td><input type='submit' class='button' id='checkoutUser' name='checkoutUser' value='Checkout'></td>";
                                echo "<td><input type='hidden' aria-hidden='true' name='id' value='" . $row['id'] . "'></td>";
                                echo "</tr>";
                }
             echo "</tbody>";   
             echo "</table>";
        echo "</form>";

                mysqli_close($VisitorManagement);
            ?>  
    </div>
  </div>
    <script src="js/vendor/jquery.min.js"></script>
    <script src="js/vendor/what-input.min.js"></script>
    <script src="js/foundation.min.js"></script>
    <script src="js/app.js"></script>
  </body>
</html>

Any ideas on why this may be happening?

  • 写回答

1条回答 默认 最新

  • dongping1689 2018-08-03 14:24
    关注

    I had made a comment, but it's easier to just answer here.

    You can only have one unique id attribute. The way this is outputting you'll have multiple forms with the same submit id. You could fix it by doing id="checkoutUser$row['id']" like this:

    echo '<td><input type="submit" class="button" id="checkoutUser' . $row['id'] . '" name="checkoutUser" value="Checkout"></td>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?