簡體   English   中英

當第二個表的 id 依賴於第一個表時,使用 php 一次插入兩個表

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

我想一次插入兩個表。我可以通過 php 更新。

例如:-我將添加 id 為 1 的第一個表的行,我將添加第二個表的 3 行。我希望所有 3 行的 id 都為 1。但我不知道該怎么做?

意味着當第一個表被更新時,最后一行 id 應該被更新到第二個表到當時更新的所有行。

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

要創建我在以下查詢中使用的表,

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 ;

它給出這樣的輸出:

在 run_test(第一張表)中,

電子

在第二個表中:

n

但在第二個表中,我想要這樣的輸出:

噸

我已經這樣做了,它運行良好。 工作正常。

(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM