简体   繁体   English

选择,插入MySQL和PHP

[英]Select, Insert MySQL and 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. 例如,我搜索了以J开头的名称,并列出了所有以J开头的名称以及登录时间,但尚未注销。 Meaning, the timeOut has been set to null from the first place. 这意味着,timeOut从一开始就被设置为null。 It displays like: 它显示如下:

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

The (timeOut) is a input type="button" . (timeOut)是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. 我想要的是每当我单击注销时,例如,单击Johny的timeOut,它将为超时插入now()值,而不影响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. 我不是真的很擅长php,所以请您忍受一下。 Thanks for your help. 谢谢你的帮助。

I think you meant to update not to insert, and you have to feed the userid to update that particular row. 我认为您打算更新而不是插入,并且您必须提供用户ID来更新该特定行。

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

You need to put a record ID of some sort in the 'value' portion of your input. 您需要在输入的“值”部分中放入某种记录ID。 Not just "log out". 不只是“注销”。 You need to post "log out jess" (or better yet, just "Jess" so you don't have to edit it when you retrieve it to remove "Log Out" from the string). 您需要发布“登出jess”(或者更好的是,仅发布“ Jess”,因此在检索它以从字符串中删除“登出”时不必编辑它)。 Usually this is done with a primary key field. 通常,这是通过主键字段完成的。 But it can also be done with any unique field. 但也可以在任何唯一字段中完成。 So instead of value='Log Out' try value='Jess' . 因此,请使用value='Log Out' value='Jess'代替value='Log Out' value='Jess'

Then $_POST['logout'] will actually contain Jess and you can build your update sql query from it. 然后$_POST['logout']实际上将包含Jess ,您可以从中构建更新sql查询。

Another method would be to skip the form entirely and just build a web link on the "Log Out" next to "Jess" which would contain "GET" parameters instead of POST. 另一种方法是完全跳过表单,仅在“ Jess”旁边的“注销”上构建一个Web链接,其中将包含“ GET”参数而不是POST。 May be easier on the eye. 在眼睛上可能会更容易。

Instead of a select statement you need to use an update statement and use a where clause to scope the record to the current user based off user id. 代替select语句,您需要使用update语句,并使用where子句根据用户ID将记录的作用域限定为当前用户。

So for you it would be like this: 因此,对您而言,它将像这样:

UPDATE records
SET logouttime=now()
WHERE userid='4'

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

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