简体   繁体   English

当第二个表的 id 依赖于第一个表时,使用 php 一次插入两个表

[英]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.我想一次插入两个表。我可以通过 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?例如:-我将添加 id 为 1 的第一个表的行,我将添加第二个表的 3 行。我希望所有 3 行的 id 都为 1。但我不知道该怎么做?

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.意味着当第一个表被更新时,最后一行 id 应该被更新到第二个表到当时更新的所有行。

My php code is,我的 php 代码是,

$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),在 run_test(第一张表)中,

电子

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, (1).我已经删除了触发器(DROP TRIGGER trigger_from_runtest2 ;) 2().在php中我已经这样使用了,

$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']);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MYSQL,PHP:仅当项目ID不在第二个表中时,才从表1插入INTO table2 - MYSQL, PHP: INSERT INTO table2 from table 1 only if the project ID is not in second table mysql使用第二张表中的auto_increment值作为第二张表中的外键插入到两个表中 - mysql insert into two tables using auto_increment value from the first table as a foreign key in the second 在使用 php 同时按 id 在 mysql 中删除之前,在另一个表中插入旧数据 - insert old data on another table before deleting in mysql using php at the same time by id 使用LAST_INSERT_ID()在PHP / MySQL中进行两次插入,并从另一个表中进行行 - Two inserts in PHP/MySQL using LAST_INSERT_ID() and rows from another table 如何在PHP-MySQL中联接两个表,该表与第二个表中的ID匹配并仍在表1中显示特定记录? - How to join two tables in PHP - MySQL that match an ID in the second table and still display specific record in table 1? 如何使用php mysql将行ID插入另一个表字段? - How to insert row id to another table field using php mysql? 如何使用 php 和 mySQL 获取客户 ID 并将其插入另一个表 - How to get customer ID and insert it into another table using php and mySQL 使用第一个表ID将数据插入2个表 - Insert data into 2 tables using first table ID 如何使用php将记录插入两个mysql表中? - How to insert record into two mysql table using php? php mysql如何使用mysql_insert_id()能够同时插入2个表中? - php mysql how to use mysql_insert_id() to be able to insert into 2 table at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM