sayem ali
sayem ali

Reputation: 1

How to update data in two table at a time

I want to insert date in two table at a time. one table is user table. When user will register the date will be saved. But i want, When admin will insert date in the donate form then the donation_date of tbl_user will be changed, after that donate table date and tbl_user date will be same. here is the image of 2 tables donate table User table........THANKS IN ADVANCE

This is my code

<?php 
    if (isset($_POST['passport_IC']) && isset($_POST['blood_group']) && isset($_POST['blood_bag']) && isset($_POST['donation_date'])) {
        $passport_ic=$_POST['passport_IC'];
        $blood_group=$_POST['blood_group'];
        $blood_bag=$_POST['blood_bag'];
        $donate_date=$_POST['donation_date'];



        $db=new PDO('mysql:host=localhost;dbname=mypro_bms','root','');
        $statement = $db->prepare("insert into donate(passport_ic,blood_group,blood_bag,donation_date) values(:passport_IC, :blood_group, :blood_bag, :donation_date)");
        $statement->execute([
            ':passport_IC' => $passport_ic,
            ':blood_group' => $blood_group,
            ':blood_bag' => $blood_bag,
            ':donation_date' => $donate_date


        ]);
    }
?>

<?php
    if (isset($_POST['donation_date']) && isset($_POST['passport_IC']) ){
        $passport_ic=$_POST['passport_IC'];                   
        $donate_date=$_POST['donation_date'];

        $db=new PDO('mysql:host=localhost;dbname=mypro_bms','root','');
        $statement = $db->prepare("INSERT INTO `tbl_user` (`donation_date`)
            SELECT :passport_IC
            FROM donate
            WHERE passport_IC = :passport_IC");

    }
?>

Upvotes: 1

Views: 42

Answers (1)

Strange
Strange

Reputation: 308

The tbl_user is not updating because you are executing INSERT query. When the row exists, you can not execute the INSERT query again. Try using this

$data = [
 'donation_date' => $donation_date,
 'passport_IC' => $passport_ic,
];
$sql = "UPDATE tbl_user SET donation_date=:donation_date WHERE passport_IC = :passport_IC";
$stmt= $db->prepare($sql);
$stmt->execute($data);

I haven't checked this code, but it should work.

Upvotes: 1

Related Questions