简体   繁体   中英

Insert two table at a time mysql using php when id of second table is depend on first table

I want to insert two table at a time.I am able to update via php.

For example :-I will add a row of first table with id 1 and I will add 3 rows of second table.I want to have id as 1 for all 3 rows.But I am not getting how to do this?

Means when first table get updated,last row id should get updated to second table to all the rows which are updated at that time.

My php code is,

$sql="INSERT INTO run_test2(url,gh_url,platform,machine_size,browser,image_id,security_id,instance,timer,protocol,concurrency,rampup,iteration,time_out,cache) values('".$_POST["url"]."','".$_POST['gh_url']."','".$_POST['platform']."','".$_POST['inst_type']."','".$_POST['browsers']."','".$_POST['imageid']."','".$_POST['sgid']."','".$_POST['instance']."','".$_POST['timer']."','".$_POST['protocol']."','".$_POST['concurrency']."','".$_POST['rampup']."','".$_POST['iteration']."','".$_POST['time_out']."','".$_POST['radio']."');";
for($a=0;$a<3;$a++){
$sql .="INSERT INTO file_email2(filename,email) VALUES('url.jmx','vidya@gmail.com');";
}
//mysqli_query($conn,$sql);
if (mysqli_multi_query($GLOBALS['db'], $sql)) {
      include_once "header.php";
    echo "Your Test Saved ,Successfully!";
} 
else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($GLOBALS['db']);
}

To create tables I have used below query,

create table run_test2(id int not null auto_increment,
                       url varchar(255),
                       gh_url varchar(255),
                       platform varchar(255),
                       machine_size varchar(255),
                       browser varchar(255),
                       image_id varchar(255), 
                       security_id varchar(255),
                       instance int,
                       timer varchar(255),
                       protocol varchar(255),
                       concurrency int,
                       rampup int,
                       iteration int,
                       time_out int, 
                       cache varchar(255), 
                       primary key(id));

alter table run_test2 modify column id int(11) auto_increment;

create table file_email2(id int,
                         filename varchar(255),
                         email varchar(255), 
                         foreign key(id) references run_test(id));      


DELIMITER $$
CREATE TRIGGER triggered_from_runtest2 
    AFTER insert ON run_test2
    FOR EACH ROW
BEGIN
set @lastId=(select id from run_test2 order by id desc limit 1);
    INSERT INTO  file_email2(id) values(@lastId);
END$$
DELIMITER ;

It is giving output like this:

In run_test(first table),

电子

In second table:

n

But in second table I want output like this:

吨

I have done like this it is perfectly working fine. working fine.

(1).I have removed trigger ( DROP TRIGGER triggered_from_runtest2 ;) 2().In php I have used like this,

$sql="INSERT INTO run_test2(url,gh_url,platform,machine_size,browser,image_id,security_id,instance,timer,protocol,concurrency,rampup,iteration,time_out,cache) values('".$_POST["url"]."','".$_POST['gh_url']."','".$_POST['platform']."','".$_POST['inst_type']."','".$_POST['browsers']."','".$_POST['imageid']."','".$_POST['sgid']."','".$_POST['instance']."','".$_POST['timer']."','".$_POST['protocol']."','".$_POST['concurrency']."','".$_POST['rampup']."','".$_POST['iteration']."','".$_POST['time_out']."','".$_POST['radio']."');";

$concur_sql = "SELECT  max(id) from run_test2;";
$result_concur_sql = mysqli_query($GLOBALS['db'],$concur_sql);
$row=mysqli_fetch_array($result_concur_sql);
        //maximum concurrency
$max_con=$row['max(id)'];
$max_con=$max_con+1;
for($a=0;$a<3;$a++){
$sql .="INSERT INTO file_email2(id,filename,email) VALUES('".$max_con."','url.jmx','vidya@gmail.com');";
}
//mysqli_query($conn,$sql);
if (mysqli_multi_query($GLOBALS['db'], $sql)) {
      include_once "header.php";
    echo "Your Test Saved ,Successfully!";
} 
else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($GLOBALS['db']);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM