Directly leave the mysqli page: http://php.net/manual/en/mysqli.commit.php
<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $mysqli->autocommit(FALSE); $mysqli->query("INSERT INTO table1 VALUES ('DEU', 'Bavarian', 'F', 11.2)"); $mysqli->query("INSERT INTO table2 VALUES ('DEU', 'Bavarian', 'F', 11.2)"); $mysqli->commit(); $mysqli->close();
* Edit using the prepared statements for the non-sane action:
<?php $mysqli = new mysqli("localhost", "root", "", ""); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $mysqli->autocommit(FALSE); $stmt1 = $mysqli->prepare("INSERT INTO tbl1 (id, intro) VALUES (?, ?)"); $stmt2 = $mysqli->prepare("INSERT INTO tbl2 (id, name) VALUES (?, ?)"); $str1 = 'abc'; $str2 = 'efg'; $str3 = 'hij'; $str4 = 'klm'; $stmt1->bind_param('ss', $str1, $str2); $stmt2->bind_param('ss', $str3,$str4); if ($stmt1->execute() == false) { echo 'First query failed: ' . $mysqli->error; } $stmt1->close(); if ($stmt2->execute() == false) { echo 'Second query failed: ' . $mysqli->error; } $stmt2->close(); $mysqli->close();
source share