|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-08-26 08:58 UTC] uw@php.net
[2009-09-03 01:00 UTC] php-bugs at lists dot php dot net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 20:00:01 2025 UTC |
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.phatak@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