sql - error handling when performing 2 mysql queries -


i have constructed function 2 queries performed. both of these queries insert data 2 separate tables, data related registration of user. in 1 table things username,password held , in other table stuff address, phone etc... here function:

function register_biz_user($post,$connection)      {     $name=$connection-> real_escape_string($_post['name']);     $lastname= $connection->real_escape_string($_post['lastname']);     $pass_hashed = password::hash($_post['password']);       $passwd= $connection->real_escape_string($pass_hashed);     $buztype= $connection->real_escape_string($_post['buztype']);      $usertype= $connection->real_escape_string($_post['usertype']);     $address= $connection->real_escape_string($_post['address']);     $city= $connection->real_escape_string($_post['city']);     $municipality= $connection->real_escape_string($_post['municipality']);     $url= $connection->real_escape_string($_post['wwwaddress']);     $email= $connection->real_escape_string($_post['e-mail']);     $phone= $connection->real_escape_string($_post['phone']);     $hash =$connection->real_escape_string(md5( rand(0,1000) ))  ;         $connection->set_charset("utf8");        $result1 = $connection->query("insert users values       (null,'" .$name. "','" .$lastname . "','".$email."','". $passwd."','".                          $hash."','". $usertype."')");        if (!$result1) {           throw new exception('error');          return false;                                                   }           else{$result2=$connection->query("insert business_users values            ('".$connection->insert_id."','" .$address."','".$url ."','".$phone.               "','".$city. "','".$municipality. "','".$buztype. "')");            }       if(!$result2)       {  throw new exception('error');           return false;} 

return true; }

and here problem: if @ code might notice there problem 1st query runs without problem , second throws exception or vice verca.

my point there danger db have partial data of registered user. the goal either both queries run or none runs.

how must write above code such can achieve above statement?

i hope clear enough.

use transactions: http://dev.mysql.com/doc/refman/5.0/en/commit.html

begin ... queries ... commit or rollback 

note: "or vice verca" - that's not possible. in case 2nd query never gets executed.

note2:

  • what's $post? seems unused.
  • why don't use prepared statements? escaping everyhing error prone.
  • why have procedural interface, passing $connection? should have objects know database connections... have mixed code @ least 3 different layers... not necessary bad if plan create write-once-get-rid-of-code not idea project have maintain months/years.

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -