ID: 49058 Updated by: u...@php.net Reported By: aphtk at yahoo dot com -Status: Open +Status: Feedback Bug Type: MySQLi related Operating System: Ubuntu 9.04 PHP Version: 5.2.10 -Assigned To: +Assigned To: mysql New Comment:
Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. Previous Comments: ------------------------------------------------------------------------ [2009-07-25 21:58:56] aphtk at yahoo dot com Description: ------------ Prepared statements fail but transaction commits other sql inserts and do not return a failure code. I am trying to insert to a (innodb) table with unique constraint. The 1st time insert proceeds as planned. On page refresh... 1st sql fails but no failure code is returned 2nd sql proceeds and inserts successfully 3rd sql fails but still no code is rolled back Reproduce code: --------------- $u = new userDAO(); //$u->createNewUser($first_name, $last_name, $username, $encrypted_passwd, $reminder_question, $reminder_hint); $u->createNewUser("Anang1", "Phatak1", "anang.pha...@gmail.com", 1, "quake2", "Is this a test ?", "test"); userDAO.php <?php require_once("Connection.php"); class userDAO { private $_connection; public function __construct() { $this->_connection = Connection::getConnection("localhost", "terminator", "terminate"); } public function readUser($username) { $sql = "SELECT * FROM xt_user_details_vw WHERE username = ?"; if ($stmt = $this->_connection->prepare($sql)) { $stmt->bind_param("s",$username); $stmt->execute(); $stmt->store_result(); $meta = $stmt->result_metadata(); $fields = array(); while ($field = $meta->fetch_field()) { $fields[] = &$row[$field->name]; } call_user_func_array(array($stmt, 'bind_result'), $fields); $datagrid = array(); $row_num = 0; while ($stmt->fetch()) { $datagrid[$row_num] = array(); foreach ($row as $k=>$v) { $datagrid[$row_num][$k] = $v; } } $stmt->free_result(); $stmt->close(); return $datagrid; } else { throw new Exception("Error occured while attempting to run user read query (userDAO.php)","56"); } } protected function addNewUserInfo($first_name, $last_name, $username, $active_flag=1){ $user_insrt_sql = "INSERT INTO xtusers (first_name, last_name, username, active_flag) VALUES (?, ?, ?, ?)"; try { if ($stmt = $this->_connection->prepare($user_insrt_sql)) { $stmt->bind_param("sssi", $first_name, $last_name, $username, $active_flag); $stmt->execute(); $stmt->close(); return $this->_connection->insert_id; } else { throw new Exception("SQL Error"); } } catch (Exception $err) { $this->_connection->rollback(); echo ($err->getMessage()); throw $err; } } protected function addPasswordInfo($encrypted_passwd) { $password_insrt_sql = "INSERT INTO xtpasswords (passwd) VALUES (?)"; try { if ($stmt = $this->_connection->prepare($password_insrt_sql)) { $stmt->bind_param("s", $encrypted_passwd); $stmt->execute(); $stmt->close(); echo ($this->_connection->sqlstate."<br/>"); return $this->_connection->insert_id; } else { throw new Exception("SQL Error"); } } catch (Exception $err) { $this->_connection->rollback(); echo ($err->getMessage()); throw $err; } } protected function addPasswordMetadata($user_id, $passwd_id, $reminder_question, $reminder_hint) { $usrpass_insrt_sql = "INSERT INTO xtuser_passwords "; $usrpass_insrt_sql .= "(user_id, passwd_id, reminder_question, reminder_hint)"; $usrpass_insrt_sql .= "values (?, ?, ?, ?)"; try { if ($stmt = $this->_connection->prepare($usrpass_insrt_sql)) { echo ($usrpass_insrt_sql."<br />"); echo ("'".$user_id."','".$passwd_id."','".$reminder_question."','".$reminder_hint."'"); $stmt->bind_param("iiss", $user_id, $passwd_id, $reminder_question, $reminder_hint); $stmt->execute(); $stmt->close(); return $this->_connection->insert_id; } else { throw new Exception("SQL Error"); } } catch(Exception $err) { $this->_connection->rollback(); echo ($err->getMessage()); throw $err; } } public function createNewUser($first_name, $last_name, $username, $active_flag, $encrypted_passwd, $reminder_question, $reminder_hint){ $this->_connection->autocommit(FALSE); try { $user_insrt_sql = "INSERT INTO xtusers (first_name, last_name, username, active_flag) VALUES (?, ?, ?, ?)"; $stmt = $this->_connection->prepare($user_insrt_sql); $stmt->bind_param("sssi", $first_name, $last_name, $username, $active_flag); $stmt->execute(); $stmt->close(); $tmp_user_id = $this->_connection->insert_id; echo ("Added user id :". $tmp_user_id ."<br />"); $password_insrt_sql = "INSERT INTO xtpasswords (passwd) VALUES (?)"; $stmt = $this->_connection->prepare($password_insrt_sql); $stmt->bind_param("s", $encrypted_passwd); $stmt->execute(); $tmp_passwd_id = $this->_connection->insert_id; echo ("Added password id :". $tmp_passwd_id ."<br />"); $usrpass_insrt_sql = "INSERT INTO xtuser_passwords "; $usrpass_insrt_sql .= "(user_id, passwd_id, reminder_question, reminder_hint)"; $usrpass_insrt_sql .= "VALUES (?, ?, ?, ?)"; $stmt = $this->_connection->prepare($usrpass_insrt_sql); $stmt->bind_param("iiss", $tmp_user_id, $tmp_passwd_id, $reminder_question, $reminder_hint); $stmt->execute(); $stmt->close(); $tmp_passwdmetadata_id = $this->_connection->insert_id; echo ("Added password metadata id :". $tmp_passwdmetadata_id ."<br />"); $this->_connection->commit(); } catch(exception $err) { $this->_connection->rollback(); echo ("<hr />". "an exception occurred".$err->getMessage()); } } public function updateUser($user_id, $first_name, $last_name, $username, $encrypted_passwd, $reminder_question, $reminder_hint) { } public function deleteUser() { } } ?> Expected result: ---------------- sql should be rolled back Actual result: -------------- 1st sql fails but no failure code is returned 2nd sql proceeds and inserts successfully 3rd sql fails but still no code is rolled back ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=49058&edit=1