PHP/MySQL 插入问题
如何向两个或多个表中插入数据?
目前我正在使用这个技巧。 (假设语法是正确的)
$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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用多个查询是完全可以的。
不过,您应该将 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
我认为这就是答案: http://dev .mysql.com/doc/refman/5.1/en/example-foreign-keys.html
This is the answer, I think : http://dev.mysql.com/doc/refman/5.1/en/example-foreign-keys.html
对于 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