why my transaction doesn't work - El Forum - 05-23-2012
[eluser]Mario "St.Peter" Valentino[/eluser]
this is my code
Code: class Pembelian_model extends CI_Model
{
public function pembelian_save($katalog_id,$nama,$email,$telepon,$handphone,$alamat,$pesan)
{
$this->db->trans_start();
$user_id = date("Ymd").'-';
$max_user = $this->check_max_user($user_id);
if($max_user=='')
{
$urutan = 1;
$user_id .= $urutan;
}
else
{
$urutan = $max_user+1;
$user_id .= $urutan;
}
$data = array('id_client' => $user_id,
'nama' => $nama,
'email' => $email,
'telp' => $telepon,
'handphone' => $handphone,
'alamat' => $alamat,
'remark' => $pesan,
'urutan' => $urutan
);
if($this->db->insert('tbl_client',$data))
{
$order_id = $katalog_id.'-'.date("Ymd").'-';
$max_order = $this->check_max_order($order_id);
if($max_order=='')
{
$urutan_order = 1;
$order_id .= $urutan_order;
}
else
{
$urutan_order = $max_order+1;
$order_id .= $urutan_order;
}
$data_order = array('id_order' => $order_id,
'id_client' => $user_id,
'id_katalog' => $katalog_id,
'tgl_order' => date("Y-m-d h:m:s"),
'status' => 0,
'urutan' => $urutan_order
);
if($this->db->insert('tbl_order',$data_order))
{
$no = 1;
foreach($this->cart->contents() as $items)
{
$id_produk = $items['id'];
$qty = $items['qty'];
$price = $items['price'];
$data = array('id_order' => $order_id,
'id_produk' => $id_produk,
'qty' => $qty,
'harga' => $price,
'urutan' => $no
);
if($this->db->insert('tbl_cart',$data))
{
if($this->db->query("UPDATE tbl_produk SET produk_stok = produk_stok-".$qty." WHERE produk_id='".$id."'"))
$result = TRUE;
else
$result = FALSE;
}
$no++;
}
}
}
$this->db->trans_complete();
if ($this->db->trans_status()===FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
return $result;
}
public function check_max_user($user_id)
{
$sql = "SELECT max(urutan) as max_sort FROM tbl_client WHERE id_client LIKE '".mysql_real_escape_string($user_id)."%'";
$query = $this->db->query($sql);
$data = $query->row();
//echo $sql;exit;
return $data->max_sort;
}
public function check_max_order($order_id)
{
$sql = "SELECT max(urutan) as max_sort FROM tbl_order WHERE id_order LIKE '".mysql_real_escape_string($order_id)."%'";
$query = $this->db->query($sql);
$data = $query->row();
//echo $sql;exit;
return $data->max_sort;
}
}
please see $id on
Code: if($this->db->query("UPDATE tbl_produk SET produk_stok = produk_stok-".$qty." WHERE produk_id='".$id."'"))
this is not exists so transaction will fail too but why transaction still saving data before the code running.
Please help me
why my transaction doesn't work - El Forum - 05-23-2012
[eluser]InsiteFX[/eluser]
Because you are mixing the two different types of transactions!
Auto and Manual.
Code: // Auto
$this->db->trans_start();
// your queries
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
// generate an error... or use the log_message() function to log your error
}
// Manual
$this->db->trans_begin();
// your queries
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
}
else
{
$this->db->trans_commit();
}
why my transaction doesn't work - El Forum - 05-23-2012
[eluser]Mario "St.Peter" Valentino[/eluser]
Code: public function pembelian_save($katalog_id,$nama,$email,$telepon,$handphone,$alamat,$pesan)
{
$this->db->trans_begin();
$user_id = date("Ymd").'-';
$max_user = $this->check_max_user($user_id);
if($max_user=='')
{
$urutan = 1;
$user_id .= $urutan;
}
else
{
$urutan = $max_user+1;
$user_id .= $urutan;
}
if($this->db->query("INSERT INTO tbl_client(id_client,
nama,
email,
telp,
handphone,
alamat,
remark,
urutan)
VALUES ('".$user_id."',
'".$nama."',
'".$email."',
'".$telepon."',
'".$handphone."',
'".$alamat."',
'".$pesan."',
'".$urutan."')"))
{
$order_id = $katalog_id.'-'.date("Ymd").'-';
$max_order = $this->check_max_order($order_id);
if($max_order=='')
{
$urutan_order = 1;
$order_id .= $urutan_order;
}
else
{
$urutan_order = $max_order+1;
$order_id .= $urutan_order;
}
if($this->db->query("INSERT INTO tbl_order (id_order,
id_client,
id_katalog,
tgl_order,
status,
urutan)
VALUES ('".$order_id."',
'".$user_id."',
'".$katalog_id."',
'".date("Y-m-d h:m:s")."',
'0',
'".$urutan_order."')"))
{
$no = 1;
foreach($this->cart->contents() as $items)
{
$id_produk = $items['id'];
$qty = $items['qty'];
$price = $items['price'];
if($this->db->query("INSERT INTO tbl_cart (id_order,id_produk,qty,harga,urutan)
VALUES ('".$order_id."',
'".$id_produk."',
'".$qty."',
'".$price."',
'".$no."')"))
{
$this->db->query("UPDATE tbl_produk SET produk_stok = produk_stok-".$qty." WHERE produk_id='".$id."'");
}
$no++;
}
}
}
//$this->db->trans_complete();
if($this->db->trans_status()===FALSE)
{
$this->db->trans_rollback();
//return false;
}
else
{
$this->db->trans_commit();
//return true;
}
}
i try this but when syntax error it still saving not rollback.
please give solution
the error syntax is in $id is not exists
|