0

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? 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. “balance” contains these rows: facebook_id, gold_balance.

The same value for “facebook_id” should inserted into both tables.
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:

$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.

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). 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

The following SQL works when I enter it directly into phpMyAdmin’s SQL console:

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:

//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? I’m putting $sql= before the Transaction statement because I need to check that $sql completed correctly before closing the database connection. 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? 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);
Community
  • 1
  • 1
Emily
  • 1,151
  • 4
  • 21
  • 42
  • Write two separate queries for the each table but use the same value for facebook_id which is stored in a variable – Rabbiya Shahid Apr 11 '15 at 19:48
  • The reference has "the correct syntax". Not trying something is not equivalent to having a problem with something tried. – user2864740 Apr 11 '15 at 20:12
  • Hi @RabbiyaShahid thank you for your reply, I was thinking of doing it that way but I need to echo the successful insert statement which then initiates a popup based on the response, I thought having both inserts happen at the same time in the same statement would be better for this, I was also concerned about an interruption/error occuring in between the two statements and causing an incomplete database update. – Emily Apr 12 '15 at 12:01
  • @user2864740 I understand it's annoying when people ask questions without first trying things, but as I said when I asked my question, I have been trying to use the methods from the link I gave, but they're not working for me, that's why I said that I've never used the Transaction method before, it's completely new to me, and I think I'm not implementing it correctly. – Emily Apr 12 '15 at 12:16
  • [here](http://stackoverflow.com/questions/2708237/php-mysql-transactions-examples) are many examples of using transactions. Try them – Barada Apr 13 '15 at 08:41
  • Hi @Barada thanks for the link, I've tried them and one of them is working correctly for me now when I enter it directly into the sql console, however it's not working in my .php script. I wrote a second update to my question with more details, if you could take a look that would be awesome! – Emily Apr 14 '15 at 13:34
  • Hi again @Barada I've just got it working, I needed to change ``$result = mysqli_query($conn, $sql);`` to ``$result = mysqli_multi_query($conn, $sql);`` Thanks again for your help, I'll mark yours as the correct answer. – Emily Apr 14 '15 at 19:08

1 Answers1

2

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.

Barada
  • 252
  • 2
  • 10
  • The key point is to use a single transaction (the default isolation level in InnoDB is RR). Then, from within other (implicit) transactions, the updates will appear 'simultaneously'. – user2864740 Apr 11 '15 at 20:11
  • Hi @Barada, thanks for your reply, I'm trying your method but it's still not inserting correctly, I noticed that you have an extra ``"`` at the end of the 2nd last line, so I've tried enclosing each insert statement in ``"`` marks, but there's still something wrong, I've updated my question with what I'm currently trying and would really appreciate any advice on it, thanks! – Emily Apr 12 '15 at 11:46