[英]SQL - How to Insert same value into two tables simultaneously
EDIT This answer has been solved, the correct code is at the bottom of the question. 编辑此答案已解决,正确的代码在问题的底部。 I hope it's useful to whoever arrives at this page with the same problem :) ~~
我希望对遇到相同问题的人有用:) ~~
I'm trying to have some data insert into two tables in my database at the same time. 我正在尝试将一些数据同时插入数据库中的两个表中。 I've tried using the method from here: SQL Server: Is it possible to insert into two tables at the same time?
我尝试从此处使用该方法: SQL Server:是否可以同时插入两个表中? but I don't think I'm implementing it correctly.
但是我认为我没有正确实现它。
The tables are named “player” and “balance”. 这些表分别命名为“玩家”和“平衡”。 “player” contains these rows:
facebook_id, first_name, last_name.
“玩家”包含以下行:
facebook_id, first_name, last_name.
“balance” contains these rows: facebook_id, gold_balance.
“余额”包含以下行:
facebook_id, gold_balance.
The same value for “facebook_id” should inserted into both tables. 应将“ facebook_id”的相同值插入两个表中。
I thought maybe this Insert statement would work but it's not: 我以为这个插入语句也许行得通,但事实并非如此:
$sql="INSERT INTO player, balance
(facebook_id, first_name, last_name, gold_balance)
VALUES
('$userid', '$userfirst_name', '$userlast_name', “100”)";
I've never used the “Transaction” method, and was wondering if that would be a suitable method to use? 我从未使用过“交易”方法,并且想知道这是否适合使用? And if so, what would be the correct syntax to implement it?
如果是这样,实现它的正确语法是什么? Thanks in advance for any help with this!
在此先感谢您的任何帮助!
Update After reading @Barada's comment this is what I'm currently trying: 更新阅读@Barada的评论后,这就是我目前正在尝试的方法:
$sql=START TRANSACTION;
"INSERT INTO player
(facebook_id, first_name, last_name) VALUES
('$userid', '$userfirst_name', '$userlast_name')"
"INSERT INTO balance
(facebook_id, gold_balance)
VALUES
('$userid', "100")"
commit;
The values for $userid, $userfirst_name, $userlast_name are correct (they enter correctly if I just use a single Insert statement, so the problem is probably with the syntax of the Transaction statement. $ userid,$ userfirst_name,$ userlast_name的值是正确的(如果我仅使用单个Insert语句,则它们输入正确,因此问题可能出在Transaction语句的语法上。
This Insert statement is contained in a PHP file that's called using AJAX during an onClick event (I'm not sure if that affects the Insert syntax required, but I thought I should mention that), also, I have the following check at the end of the PHP file to send an echo back to the AJAX function that called the PHP file (I then initiate a popup depending on whether it's a "successful" or "failed" response). 此Insert语句包含在一个PHP文件中,该文件在onClick事件中使用AJAX调用(我不确定这是否会影响所需的Insert语法,但我想我应该提一下),此外,我在末尾进行了以下检查的PHP文件,以将回声发送回名为PHP文件的AJAX函数(然后根据响应是“成功”还是“失败”,启动弹出窗口)。 I was wondering if this could negatively affect the Insert operation?
我想知道这是否会对插入操作产生负面影响?
//If the SQL insert statement completed correctly:
$result = mysqli_query($conn, $sql);
if ($result) {
$verify=1;
}
else{
$verify=0;
}
echo $verify;
mysqli_close($conn);
Update 2 I thought I should mention what I'm using to test this: I'm working with WAMP on localhost, my PHP version is 5.6.5 and mySQL version is 5.6.12 更新2我以为我应该提到我要用来测试的内容:我正在localhost上使用WAMP,我的PHP版本是5.6.5,而mySQL版本是5.6.12
The following SQL works when I enter it directly into phpMyAdmin's SQL console: 当我直接将其输入phpMyAdmin的SQL控制台时,以下SQL可以工作:
START TRANSACTION;
INSERT INTO player (facebook_id, first_name, last_name) VALUES ('test_id', 'test_firstname', 'test_lastname');
INSERT INTO balance (facebook_id, gold_balance) VALUES ('test_id', '100');
COMMIT;
It updates the tables perfectly. 它完美地更新了表。 However when I try to use the following in my .php script it's failing to enter anything:
但是,当我尝试在.php脚本中使用以下代码时,则无法输入任何内容:
//After database connection has taken place…
$sql=START TRANSACTION;
INSERT INTO player (facebook_id, first_name, last_name) VALUES ('test_id', 'test_firstname', 'test_lastname');
INSERT INTO balance (facebook_id, gold_balance) VALUES ('test_id', '100');
COMMIT;
//If the SQL insert statement completed correctly...
$result = mysqli_query($conn, $sql);
if ($result) {
$verify=1;
}
else{
$verify=0;
}
echo $verify;
mysqli_close($conn);
Since the SQL works perfectly when entered directly into phpMyAdmin's SQL console, does that mean that the problem is an error in my PHP code? 由于直接输入phpMyAdmin的SQL控制台时SQL可以完美地工作,这是否意味着问题出在我的PHP代码中? I'm putting
$sql=
before the Transaction statement because I need to check that $sql
completed correctly before closing the database connection. 我将
$sql=
放在Transaction语句之前,因为我需要在关闭数据库连接之前检查$sql
是否正确完成。 But could this be why it's not working? 但这可能是为什么它不起作用的原因吗? I've tried omitting it, and have tried adding and removing
()
“”
;
我尝试省略它,并尝试添加和删除
()
“”
;
marks in the hope that there's just one little thing missing but so far nothing has worked. 标志着希望只有一件事,但到目前为止没有任何效果。 Could using
mysqli_query
be the reason it's not working? 能否使用
mysqli_query
是它无法正常工作的原因? Any help would be much appreciated! 任何帮助将非常感激!
The following code works! 以下代码有效! :)
:)
$sql = "INSERT INTO player (facebook_id, first_name, last_name) VALUES
('$userid', '$userfirst_name', '$userlast_name');";
$sql .= "INSERT INTO balance (facebook_id, gold_balance)
VALUES ('$userid', '100');";
$result = mysqli_multi_query($conn, $sql);
if ($result) {
$verify=1;
}
else{
$verify=0;
}
echo $verify;
mysqli_close($conn);
You are right. 你是对的。 For this case transactions were invented.
在这种情况下,发明了交易。 Try this:
尝试这个:
START TRANSACTION;
INSERT INTO player
(facebook_id, first_name, last_name, gold_balance)
VALUES
('$userid', '$userfirst_name', '$userlast_name', “100”)
INSERT INTO BALANCE
(facebook_id, first_name, last_name, gold_balance)
VALUES
('$userid', '$userfirst_name', '$userlast_name', “100”)"
commit;
Fix insert statements to fit tables. 修复插入语句以适合表格。
But i see no problems to use two separate queries like you were already suggested. 但是我认为使用两个独立的查询没有问题,就像已经建议过的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.