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.
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>';
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.
So for you it would be like this:
UPDATE records
SET logouttime=now()
WHERE userid='4'
You need to put a record ID of some sort in the 'value' portion of your input. 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). 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'
.
Then $_POST['logout']
will actually contain Jess
and you can build your update sql query from it.
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. May be easier on the eye.