Sofia Benkirane
Sofia Benkirane

Reputation: 13

SQL Select/ Insert / Update query PHP MySQL

I have a table with columns userID(int),timeIN(date),timeOUT(date)

I am inserting a record in mysql database. First I check if the userID is correct from the other table and if its correct it will add a new record of the userID and timeIN(date) whereas the timeOUT will be NULL, else it will display error if the userID is not correct. I want my code to be able to check if the user is currently timeIN so it will prevent a double entry. I would also like to insert or update timeOUT(date) if the values of userID is equals to the user input and timeIN is not null and timeOUT is null.

Please kindly help...thanks.

Here is my code for inserting userID and timeIN: IT WORKS when inserting into mysql database.

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
require_once('dbConnect.php');

$userID = $_POST['userID'];
$sql = "SELECT * FROM employee WHERE userID='$userID'";
$result = mysqli_query($con,$sql);
$check = mysqli_fetch_array($result);

if(isset($check)){
    $sql = "INSERT INTO dtr (userID,timeIN) VALUES ('$userID', now())";
    mysqli_query($con, $sql);
    echo 'Time IN Successful!';
    }else{
        echo 'Invalid USER ID. Please try again!';
        }           
 mysqli_close($con);
}
?>

Upvotes: 0

Views: 1414

Answers (2)

Ali Rasheed
Ali Rasheed

Reputation: 2817

I did it from my mobile not tested but you will get the idea of what is going on

if(isset($check))
{

$sql="SELECT * FROM dtr WHERE userID = $userID";
$result = mysqli_query($con,$sql);
$check = mysqli_fetch_array($result);

if(isset($check))
{
    echo "Already in";
    if(isset($check['timeIN']) && !isset($check['timeOUT']))
    {
        $sql = "UPDATE dtr SET timeOUT= now() WHERE userID=$userID";
        mysqli_query($con, $sql);
        mysqli_close($con);
    }
}
else
{
    $sql = "INSERT INTO dtr (userID,timeIN) VALUES ('$userID', now())";
    mysqli_query($con, $sql);
    mysqli_close($con);
    echo 'Time IN Successful!';
}
}
else
{
echo 'Invalid USER ID. Please try again!';
mysqli_close($con);
}           

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269763

You should handle these checks inside the database. The current check you are doing in the database can be handled by a foreign key constraint:

alter table dtr add constraint fk_dtr_userId
    foreign key (userId) references employee(userId);

The second means that you want only one row with a NULl value. Ideally, this could be handled with a unique constraint:

alter table dtr add constraint unq_dtr_userId_timeOut
    unique (userId, timeOut);

Unfortunately (for this case), MySQL allows duplicate NULL values for unique constraints. So, you can do one of two things:

  • Use a default value, such as '2099-12-31' for time out.
  • Use a trigger to enforce uniqueness

In either case, the database itself will be validating the data, so you can be confident of data integrity regardless of how the data is inserted or updated.

Upvotes: 1

Related Questions