I create if statement inside while loop .
And I have three rows of data in table like below
row 1 : **timeout 0830 , timein 1030**
row 2 : **timeout 1230 , timein 1300**
row 3 : **timeout 1400 , timein 1730**
The problem is , the output show like this
Time added ! Duplicate Time added
And the data still added in the 1st and 3rd row. I don't know why.
What I want is to display errors if any one of the rows is duplicate without adding the data.
Let say I input data for the 2nd row which is 1230 and 1300. I want the output to appear as:
Duplicate
And 1st and 2nd row will not adding any data since one the rows is duplicate.
Any solution ?
<?php
$connect = mysqli_connect("localhost", "root", "", "movementandroid");
global $connect;
if(isset($_POST['Submit'])){
$user_id = $_POST['user_id'];
$timeout = $_POST['timeout'];
$timein = $_POST['timein'];
$sql = "SELECT * FROM table WHERE user_id='$user_id'";
$get = mysqli_query($connect, $sql);
if($get && mysqli_num_rows($get) > 0 ){
while($run2 = mysqli_fetch_assoc($get)){
$timeout_new = $run2['timeout'];
$timein_new = $run2['timein'];
if(($timeout >= $timeout_new) && ($timein <= $timein_new)){
echo "Duplicate !";
}
else{
$add = "INSERT INTO table (timeout, timein)
VALUES ('$timeout', '$timein')";
$addDateTime = mysqli_query($connect,$add);
echo "Time added !";
}
}
mysqli_free_result($get);
}
}
?>
<form action="dd.php" method="post">
<table>
<tr>
<td><i class="fa fa-unlock-alt"></i> </td>
<td>User ID : </td>
<td><input type ="text" name="user_id" size="30"></td>
</tr>
<tr>
<td><i class="fa fa-unlock-alt"></i> </td>
<td>Time out : </td>
<td><input type ="text" name="timeout" size="30"></td>
</tr>
<tr>
<td><i class="fa fa-unlock-alt"></i> </td>
<td>Time in : </td>
<td><input type ="text" name="timein" size="30"></td>
</tr>
</table>
<p><input class="btnSuccess" type ="submit" name="Submit" value="Submit"> </p>
</form>