duandongji2231 2014-10-10 04:04
浏览 39
已采纳

选择,插入MySQL和PHP

I have created a search function and was able to display the rows.

But I have no idea what to do about this thing:

For example, I searched name starting with J, and lists all the names starting with J with login time and has not been logout. Meaning, the timeOut has been set to null from the first place. It displays like:

1 | Johny | 7:58 | (timeOut)
4 | Jess  | 8:05 | (timeOut)`

The (timeOut) is a input type="button". And I what I want is whenever I click the logout, for example, I click timeOut for Johny, it will insert values now() for the timeout, not affecting the row for Jess.

I have this code:

<?php
$result=mysqli_query($con,"SELECT  * FROM records WHERE PlateNumber LIKE '%$name%'");
while($row = mysqli_fetch_array($result)) {
    if($row['TimeOut'] == null){
     echo "<table border='1'>" . "<tr>" ."<td style='width:100px;'>" .  $row['Number']. "</td>". "<td style='width:100px;'>" .  $row['PlateNumber']. "</td>". "<td style='width:100px;'>" . $row['TimeIn'] . "</td>" ."<td style='width:100px;'>" . "<form>" . "<input type='submit' name='logout' value='Log Out'/>" . "</form>". "</td>" . "</table>" ;
            if(isset($_POST['logout'])){
                  //HELP ME IN THIS PART
            }
    }
}
?>

I'm not really good at php so please bear me with this. Thanks for your help.

  • 写回答

3条回答 默认 最新

  • doukang8949 2014-10-10 04:14
    关注

    I think you meant to update not to insert, and you have to feed the userid to update that particular row.

    Also you do not need to spawn every form in the loop. Wrap the table with the form instead.

    if(isset($_POST['logout'])) { // if logout button is submitted
        // do not insert but update
        $id = $con->real_escape_string($_POST['logout']);
        $query = $con->query("UPDATE records SET TimeOut = NOW() WHERE id = '$id'");
    }
    
    $name = $con->real_escape_string($name);
    $result = mysqli_query($con, "SELECT  * FROM records WHERE PlateNumber LIKE '%$name%'");
    
    echo '<form method="POST">';
    echo '<table boder="1" cellpadding="10">';
    
    while($row = mysqli_fetch_assoc($result)) {
        if(empty($row['TimeOut'])){
    
            $id = $row['id'];
             echo
                "<tr>" .
                    "<td style='width:100px;'>" .  $row['Number']. "</td>".
                    "<td style='width:100px;'>" .  $row['PlateNumber']. "</td>".
                    "<td style='width:100px;'>" . $row['TimeIn'] . "</td>" .
                    "<td style='width:100px;'>" .
                    "<button type='submit' name='logout' value='$id'>Logout</button>" . "</td>" .
                "</tr>";
        }
    }
    
    echo '</table>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起