noam76

Transaction

Jul 17th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.55 KB | None | 0 0
  1. try
  2.   {
  3.    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password, array(PDO::ATTR_PERSISTENT => true));
  4.   }
  5.   catch(PDOException $e)
  6.   {
  7.    echo $conn . "<br>" . $e->getMessage();
  8.    exit();
  9.   }
  10.  
  11. try
  12.   {  
  13.    // set the PDO error mode to exception
  14.    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.  
  16.    // start transaction
  17.    $conn->beginTransaction();
  18.    $conn->query("SET NAMES 'utf8'");
  19.    $q="SELECT * FROM synagogue.customer_table WHERE first_name='$first_name' AND last_name='$last_name'";
  20.    $query_res = $conn->query($q);
  21.    $count= count($query_res->fetchAll());
  22.    if($count > 0)
  23.    {
  24.     echo "מתפלל קיים";
  25.    }
  26.    
  27.    else
  28.    {  
  29.     $table_customer = "customer_table";
  30.     $table_debts = "debts_balance";
  31.    
  32.     $data_customer = array
  33.     (
  34.      "first_name" => "$first_name",
  35.      "last_name" => "$last_name",
  36.      "address" => "$address",
  37.      "tel" => "$tel",
  38.      "mail" => "$mail"
  39.     );
  40.     $sql = "INSERT INTO `$table_customer` (`".implode('`,`',array_keys($data_customer))."`) VALUES ('".implode('\',\'',array_values($data_customer))."');";
  41.  
  42.     $conn->exec($sql);
  43.    
  44. // read the last ID
  45.     $last_id = $conn->lastInsertId();
  46.     $date = date('Y-m-d');
  47.     $sql = "INSERT INTO `synagogue`.`debts_balance` (customer_id, date, debt_balance) VALUES ('$last_id', '$date', 0)";
  48.     $conn->exec($sql);
  49.    
  50.     $conn->commit();
  51.     echo "ברוך הבא למתפלל החדש";
  52.    }
  53.   }
  54.   catch(PDOException $e)
  55.   {
  56.    $conn->rollBack();
  57.    echo $sql . "<br>" . $e->getMessage();
  58.   }
  59.   $conn = null;
  60.  
  61.  ?>
Add Comment
Please, Sign In to add comment