Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
[PHP][MySQL]如何同时在两个表中插入数据?_Php_Mysql - Fatal编程技术网

[PHP][MySQL]如何同时在两个表中插入数据?

[PHP][MySQL]如何同时在两个表中插入数据?,php,mysql,Php,Mysql,我在两个表中插入数据有点问题,需要一些帮助 例如: 表1: CREATE TABLE tb1 ( tb1_id int(5) not null AUTO_INCREMENT PRIMARY KEY, tb1_title varchar(50), tb1_cat varchar(50) ); CREATE TABLE tb2 ( tb2_id int(5) not null AUTO_INCREMENT PRIMARY KEY, tb2_title var

我在两个表中插入数据有点问题,需要一些帮助

例如:

表1:

CREATE TABLE tb1 (
    tb1_id int(5) not null AUTO_INCREMENT PRIMARY KEY,
    tb1_title varchar(50),
    tb1_cat varchar(50)
);
CREATE TABLE tb2 (
    tb2_id int(5) not null AUTO_INCREMENT PRIMARY KEY,
    tb2_title varchar(50),
    tb2_doc varchar(200),
    id_tb1 int(5) not null REFERENCES tb1(tb1_id)
);
$sqla = "INSERT INTO tb_1 (tb1_title, tb1_cat) VALUES ('$tb1_title', '$tb1_cat')";
$sqlb = "INSERT INTO tb_2 (tb2_title, tb2_doc, <b>[? ? ?]</b>) VALUES ('$tb2_title', '$tb2_doc', <b>[? ? ?]</b>)";

mysqli_query($db, $sqla);
mysqli_query($db, $sqlb);
表2:

CREATE TABLE tb1 (
    tb1_id int(5) not null AUTO_INCREMENT PRIMARY KEY,
    tb1_title varchar(50),
    tb1_cat varchar(50)
);
CREATE TABLE tb2 (
    tb2_id int(5) not null AUTO_INCREMENT PRIMARY KEY,
    tb2_title varchar(50),
    tb2_doc varchar(200),
    id_tb1 int(5) not null REFERENCES tb1(tb1_id)
);
$sqla = "INSERT INTO tb_1 (tb1_title, tb1_cat) VALUES ('$tb1_title', '$tb1_cat')";
$sqlb = "INSERT INTO tb_2 (tb2_title, tb2_doc, <b>[? ? ?]</b>) VALUES ('$tb2_title', '$tb2_doc', <b>[? ? ?]</b>)";

mysqli_query($db, $sqla);
mysqli_query($db, $sqlb);
tb1
的一个条目可以有许多
tb2
的信息(行),但是如何在
tb2
的一些行中插入
tb1
的id

formular.php:

CREATE TABLE tb1 (
    tb1_id int(5) not null AUTO_INCREMENT PRIMARY KEY,
    tb1_title varchar(50),
    tb1_cat varchar(50)
);
CREATE TABLE tb2 (
    tb2_id int(5) not null AUTO_INCREMENT PRIMARY KEY,
    tb2_title varchar(50),
    tb2_doc varchar(200),
    id_tb1 int(5) not null REFERENCES tb1(tb1_id)
);
$sqla = "INSERT INTO tb_1 (tb1_title, tb1_cat) VALUES ('$tb1_title', '$tb1_cat')";
$sqlb = "INSERT INTO tb_2 (tb2_title, tb2_doc, <b>[? ? ?]</b>) VALUES ('$tb2_title', '$tb2_doc', <b>[? ? ?]</b>)";

mysqli_query($db, $sqla);
mysqli_query($db, $sqlb);
$sqla=“插入tb_1(tb1_标题,tb1_目录)值(“$tb1_标题”,“$tb1_目录”)”;
$sqlb=“插入tb_2(tb2_标题、tb2_文档、[???])值(“$tb2_标题”、“$tb2_文档”、“[???]”);
mysqli_查询($db,$sqla);
mysqli_查询($db,$sqlb);

我必须在这里更改什么?

您可以使用获取
tb1\u id
的值,然后将其插入
tb2

$sqla = "INSERT INTO tb1 (tb1_title, tb1_cat) VALUES ('$tb1_title', '$tb1_cat')";
if (mysqli_query($db, $sqla)) {
    $tb1_id = mysqli_insert_id($db);
    $sqlb = "INSERT INTO tb2 (tb2_title, tb2_doc, id_tb1) VALUES ('$tb2_title', '$tb2_doc', $tb1_id)";
    mysqli_query($db, $sqlb);
}

是的,你可以。。试试这个

BEGIN;
    INSERT INTO tb_1 (tb1_title, tb1_cat) VALUES ('$tb1_title', '$tb1_cat');
    INSERT INTO tb_2 (tb2_title, tb2_doc, <b>[? ? ?]</b>,id_tb1) VALUES ('$tb2_title', '$tb2_doc', <b>[? ? ?]</b>,LAST_INSERT_ID());
    COMMIT;
开始;
插入tb_1(tb1_标题,tb1_目录)值(“$tb1_标题”,“$tb1_目录”);
插入tb_2(tb2_标题,tb2_文档,[???],id_tb1)值(“$tb2_标题”,“$tb2_文档”,[???],最后一次插入id());
犯罪

使用
mysqli\u insert\u id()?我是数据库设计新手我不知道你是什么意思
$tb1_id
使用
mysqli_insert_id
从第一次插入中分配
tb1_id
的值,然后在第二次查询中用作
tb2
中的
id_tb1
的值(如果在代码上向右滚动,您将在
$sqlb
分配的末尾看到它)。这很好。我很高兴能帮上忙。@NageshKatke同样的建议也值得称赞。使用这段代码,它还需要使用引用tb1(tb1_id)还是必须使用引用tb1(tb1_id)?这不是必需的,但这是一件好事。例如,如果您试图删除
tb1
中引用的
tb2
中的一个条目,您将得到一个错误。当我同时插入多个数据时,mysqli\u insert\u id()会出现一些问题。。。例如,数据库中tb1中已有4篇文章,tb2中已有11篇文档。现在我想上传一篇帖子(tb1),里面有3个文档(tb2),帖子编号为5。第一个文档(将是tb2_id#12)获得正确的tb1_id#5,但第二个文件随后获得tb1_id#12。因此,mysqli_insert_id()似乎总是使用最后一个id,不管是tb1_id还是tb2_id。您有解决方案吗?