Arth Quilitis
Arth Quilitis

Reputation: 35

How to Insert values on Multiple Table on my data base

I need to insert values into multiple table. Please correct my code because it just double the inserted value on table_attendace

if(isset($_POST['text']))
    {

        $text =$_POST['text'];


        // insert query 
        $sql = "INSERT INTO table_attendance(NAME,TIMEIN) VALUES('$text',NOW())";
        $query =mysqli_query($conn,$sql) or die(mysqli_error($conn));
        
        if($query==1) 
        {
            $ins="INSERT INTO table_attendancebackup(NAME,TIMEIN) VALUES('$text',NOW())";
            $quey=mysqli_query($conn,$sql) or die(mysqli_error($conn));
                if ($quey==1) {
                    $_SESSION['success'] = 'Action Done';
                }else{
                    $_SESSION['error'] = $conn->error;
                }
        }
    }

Upvotes: 1

Views: 39

Answers (2)

Pandiyan
Pandiyan

Reputation: 23

you can write two queries in single variable with semicolon then you can save the same data in both table.

And $quey=mysqli_query($conn,$sql) or die(mysqli_error($conn)); -> in this line you placed the $ins variable wrongly instead of $ins.

$sql = "INSERT INTO table_attendance(NAME,TIMEIN) VALUES('$text',NOW()); INSERT INTO table_ttendancebackup(NAME,TIMEIN) VALUES('$text',NOW())";

$query = mysqli_query($conn, $sql) or die(mysqli_error($conn));

Upvotes: 2

afarrag
afarrag

Reputation: 342

In the second query, you reused the first query $sql again, instead of using $ins. It should be

$quey=mysqli_query($conn,$ins) or die(mysqli_error($conn));

Upvotes: 2

Related Questions