5

I know inserting into multiple tables with single query is not possible,

now I am trying to insert into 2 tables with php using START TRANSACTION but its not working.

my sql query is looks like

mysqli_query($con,"START TRANSACTION
    INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '$fname', '$lname', '$domain', '$address1', '$address2', '$city', '$country', '$region', '$zip', '$phone', '$getplan', '$duration', '$getprice', '', '0', '0', '$code', '$date', '$time','0', '', '')
    INSERT INTO domains (username) VALUES ('$getuser') COMMIT");

So where is the problem??

many thanks in advance.

3
  • 1
    I maybe wrong, but your insert statement seems wrong to me. shouldn't it be INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);? Commented Aug 12, 2014 at 11:51
  • and what you see?? isn't it seems like as you mention? Commented Aug 12, 2014 at 11:53
  • please read this on how to insert using correct syntax dev.mysql.com/doc/refman/5.6/en/insert.html Commented Aug 12, 2014 at 11:56

2 Answers 2

5

it is because mysqli_query can handle only one comand each time. Split them like:

mysqli_query($con, "SET AUTOCOMMIT=0");
mysqli_query($con,"START TRANSACTION");
$insert1 = mysqli_query($con,"INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '$fname', '$lname', '$domain', '$address1', '$address2', '$city', '$country', '$region', '$zip', '$phone', '$getplan', '$duration', '$getprice', '', '0', '0', '$code', '$date', '$time','0', '', '')");
$insert2 = mysqli_query($con,"INSERT INTO domains (username) VALUES ('$getuser')");

if($insert1 && $insert2) {
    mysqli_query($con,"COMMIT");
} else {
    mysqli_query($con,"ROLLBACK");
}
mysqli_query($con, "SET AUTOCOMMIT=1");

update: if you are using mysqli the objectorientated way, you can do the following:

$mysqli->begin_transaction();
$insert1 = $mysqli->query("INSERT INTO users VALUES ('', '$getuser', '$getpass', '$getemail', '$fname', '$lname', '$domain', '$address1', '$address2', '$city', '$country', '$region', '$zip', '$phone', '$getplan', '$duration', '$getprice', '', '0', '0', '$code', '$date', '$time','0', '', '')");
$insert2 = $mysqli->query("INSERT INTO domains (username) VALUES ('$getuser')");

if($insert1 && $insert2) {
    $mysqli->commit();
} else {
    $mysqli->rollback();
}

HINT: if you use an older PHP version as 5.5.0, you can't use $mysqli->begin_transaction(); and have to use $mysqli->autocommit(false); at the begining and $mysqli->autocommit(true); at the end instead

Sign up to request clarification or add additional context in comments.

4 Comments

is this mysql_query("SET AUTOCOMMIT=0"); or mysqli_query("SET AUTOCOMMIT=0"); ???
but all of this is separate queries, not in single query. it should in 1 query with transaction. how to do that?
yes, but because of AUTOCOMMIT is off and START TRANSACTION they will be handled in one transaction.
OK, if it works I will accept your answer. and if you think my question is helpful, you can upvote my question to help me increase my rating. thanks
0

Instead of

if($insert1 && $insert2)

name it just "insert" and then:

if ($insert->affected_rows > 0)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.