From:             aphtk at yahoo dot com
Operating system: Ubuntu 9.04
PHP version:      5.2.10
PHP Bug Type:     MySQLi related
Bug description:  Prepared statements with transaction support

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", "[email protected]", 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 bug report at http://bugs.php.net/?id=49058&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49058&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49058&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49058&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49058&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49058&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49058&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49058&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49058&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49058&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49058&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49058&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49058&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49058&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49058&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49058&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49058&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49058&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49058&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49058&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49058&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49058&r=mysqlcfg

Reply via email to