0

i have a select query that will check if the id is exist then it will fetch the data of it and then i place a select query on another table that will check if the timeout column is empty because i want the user/student to timeout first before he or she can time-in again, and if not it will execute the insert query, the insert query is working if the table student_att has value, but when i delete all the records in the student_att it is not inserting any data. hope you can help me fix my code. advance thanks.

here is my php code:

<?php 

include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid' ");
$count = mysqli_num_rows($sql);
if ($count == 0) {
            header("location:notexist.php");
        }else{

        while( $row = mysqli_fetch_array($sql)) {
                    $rfid=$row['rfid_num'];
                  $id=$row['id'];
                   $name0 = $row['name'];
                       $course0 = $row['course'];
                    $image = $row['image'];

          $sql1 = mysqli_query($conn,"select * from student_att where rfid_num ='$rfid' order by number DESC limit 1 ");
                while( $row = mysqli_fetch_array($sql)) {

             if(empty($row['timeout'])){
                   header("location:logout.php");
                 }else{


         $InsertSql = mysqli_query($conn,"INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')");
       }

       }

        }

}
}
?>
  • 1
    You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use [`mysqli_error()`](http://php.net/manual/en/mysqli.error.php) to get a detailed error message from the database. – John Conde Aug 07 '18 at 18:21
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 07 '18 at 18:21
  • @JohnConde , i tried to use mysqli_error using on the duplicate question but its not working , can you teach me how can display my error? –  Aug 07 '18 at 18:44
  • Just type `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` on top of your script (possibly on top of `connection.php`). However, you may also need to ensure that your PHP set-up actually [reports errors](https://www.phptherightway.com/#error_reporting). – Álvaro González Aug 07 '18 at 18:46
  • @ÁlvaroGonzález , i tried to use it but not working, can you my check query , if i place the insert query above sql1 select query its inserting but when i place below sql1 select query its not inserting –  Aug 07 '18 at 18:52
  • The query will fail randomly depending on input, as Quentin already told you. – Álvaro González Aug 07 '18 at 18:55

0 Answers0