tags:

views:

8

answers:

1

The function below, as its name suggests, registers/records the data from signup form into the database. In order to avoid table addresses from getting the wrong adressID(which references members(memberID)), I have to wrap the two insert queries into members and addresses respectively with autocommit and commit.

function register($un, $fname, $lname, $email, $pwd, $phone, $street, $city, $country, $postal, $state )
    {   
        $this->conn->autocommit(FALSE);

        $query_insert_members = "INSERT INTO members(fname, lname, uname, email, phone, password, joinDate) VALUES('".$fname."', '".    
                                $lname."', '".$un."', '".$email."', '".$phone."', '".md5($pwd)."', NOW())";

        $registerquery_members = $this->conn->query($query_insert_members);

        $last_id = $this->conn->insert_id;

        $query_insert_addresses = "INSERT INTO addresses(addressID, street, city, state, country, zip) VALUES('".$last_id."', '".
                            $street."', '".$city."', '".$state."', '".$country."', '".$postal."')";

        $registerquery_addresses = $this->conn->query($query_insert_addresses);

        if($this->conn->commit) 
        {
            return true;
        }
        else
        {   
            $affectd_rows = $this->conn->num_rows;
            return $affectd_rows.$this->conn->error;
            $this->conn->rollback();
        }
    }

It doesn't seem to commit anything and doesn't return any error messages either. I only get a blank message.

+1  A: 

You access the property "commit", not calling the function "commit()".

The reason you get no error is because $this->conn->commit inside the if statement will effectively check if a commit property exists in the $this->conn object which evaluates to false. You then execute the else branch where you just return affected rows concatenated with the error string which works of course without failure no matter what. Calling rollback after the return will never execute btw.

Raoul Duke