[英]Submitting data to two mysql tables using PDO
Is it possible to submit data to two tables with the same query? 是否可以使用相同的查询将数据提交到两个表?
My existing code looks like this: 我现有的代码如下:
private function adduser() {
if (!empty($this->error)) return false;
$params = array(
':user_level' => parent::getOption('default-level'),
':name' => $this->name,
':email' => $this->email,
':username' => $this->username,
':password' => parent::hashPassword($this->password)
);
parent::query("INSERT INTO `login_users` (`user_level`, `name`, `email`, `username`, `password`)
VALUES (:user_level, :name, :email, :username, :password);", $params);
I didn't write this code so it is a bit confusing to me as I don't usually use PDO. 我没有编写此代码,因此我有点困惑,因为我通常不使用PDO。 What I would like to do in addition to this is add two values to my 'url_alias' table, the first is the UID (which is auto incremented from the first query) and the second is another variable value.
除此以外,我还要在“ url_alias”表中添加两个值,第一个是UID(从第一个查询开始自动递增),第二个是另一个变量值。
All of the examples I have found while searching dont seem to work for me because of the way this existing code looks. 我在搜索过程中发现的所有示例似乎都不适合我,因为这些现有代码看起来很像。
Can anyone give me a hand? 有人可以帮我吗?
It doesn't matter what db driver your are using (PDO, Mysqli, etc.) you question is purely about mysql capabilities. 您使用的是什么数据库驱动程序(PDO,Mysqli等)都无关紧要,您所质疑的纯粹是关于mysql功能的。 Mysql may update and delete rows from multiple tables in a single query but not insert.
Mysql可以在单个查询中更新和删除多个表中的行,但不能插入。 Ie
INSERT table_1, table_2 ...
is not allowed. 即
INSERT table_1, table_2 ...
是不允许的。
You have to run one query for each table you want to insert data. 您必须对要插入数据的每个表运行一个查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.