PHP/MySQL 插入问题

发布于 2024-12-05 10:22:18 字数 425 浏览 7 评论 0原文

如何向两个或多个表中插入数据?

目前我正在使用这个技巧。 (假设语法是正确的)

$Sql1 = "insert into user";
$Sql2 = "insert into files";
$Sql3 = "insert into messages";

query $Sql1
query $$ql2
query $Sql3

所以我正在使用这个并且我需要优化。

另外,我想我需要使用外键?但我不知道如何在 phpmyadmin 上执行此操作。

最后:

这就是我想要的。

在“用户”表上插入数据,然后自动在“消息”和“文件”表上插入数据 还将有一个与用户相同的 ID。这就是为什么我在代码中使用 3 个查询。

请帮我。

提前致谢!

米布

How to insert data into two or more tables?

Currently I am using this trick. (Let just assume that the syntax is correct)

$Sql1 = "insert into user";
$Sql2 = "insert into files";
$Sql3 = "insert into messages";

query $Sql1
query $ql2
query $Sql3

So I am using this and I need to optimize.

Also, I think I need to use Foreign key? But I don't know how to do it on phpmyadmin.

and lastly:

This is all I want.

To insert data on the "user" table and then automatically the table "messages" and "files"
will have also an ID which is the same with the user.. That is why I use to 3 queries in my code.

Please help me.

Thanks in ADVANCE!

myb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

留一抹残留的笑 2024-12-12 10:22:18

使用多个查询是完全可以的。

不过,您应该将 3 个查询包装成一个事务。也就是说,如果一个查询由于某种原因失败,则可以回滚所有先前查询(如果有)的影响,以便您的数据库保持一致的状态。

注意:MySQL 的 MyISAM 表无法进行事务处理。此外,您必须使用 PHP 的 Mysqli (或 PDO)能够使用事务。 (我假设您没有使用某些高级数据库抽象层。)

至于您关于外键的问题:据我所知,它不会当您对数据库中的表使用 MyISAM 引擎时(我推测),这是有区别的,因为 MyISAM 甚至不支持外键的显式建模。然而,其他表引擎(例如 InnoDB)可以使用外键来检查某些约束,例如“消息必须始终与现有人员关联”。

以下是一些关于外键的好例子: http://www.postgresql .org/files/documentation/books/aw_pgsql/node131.html

It is perfectly fine to use multiple queries.

However, you should wrap the 3 queries into one transaction. That is, if one query fails for some reason, the effects of all previous queries (if any) can be rolled back so that your database stays in a consistent state.

Note: Transactions are not possible with MySQL's MyISAM tables. Additionally you have to use PHP's Mysqli (or PDO) to be able to use transactions. (I assume that you're not using some high-level data base abstraction layer.)

As to your question regarding foreign keys: As far as I know, it makes no difference when you're using the MyISAM engine for the tables in your database (which I presume), because MyISAM doesn't even support the explicit modeling of foreign keys. Other table engines like InnoDB, however, can use foreign keys to check certain constraints like "a message must always be associated with an existing person".

Here are some nice examples regarding foreign keys: http://www.postgresql.org/files/documentation/books/aw_pgsql/node131.html

酸甜透明夹心 2024-12-12 10:22:18

对于 mysql,你可以使用 LAST_INSERT_ID() 或在 php mysql_insert_id() 函数中。查看此手册条目:

http://dev.mysql。 com/doc/refman/5.0/en/getting-unique-id.html

http://php.net/manual/en/function.mysql-insert-id.php

With mysql u can use LAST_INSERT_ID() or in php mysql_insert_id() function. Checkout this manual entries:

http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

http://php.net/manual/en/function.mysql-insert-id.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文