Mysqli Php stored procedure transactional

Follow the steps below:
1. Create a stored procedure to the phpmyadmin
 e.g.

DROP PROCEDURE `remove_product`//
CREATE DEFINER=`root`@`localhost` PROCEDURE `remove_product`(IN inProductId INT, IN inUserId INT  , OUT ret TINYINT)
BEGIN
declare exit handler for not found rollback;
declare exit handler for sqlwarning rollback;
declare exit handler for sqlexception rollback;


set ret=0;
start transaction;

INSERT INTO product_deleted 
( product_id,  product_name ,  product_description ,  product_category_id ,  product_status ,
  product_duration,  product_price_per_day,  product_price_per_weekend,  product_price_per_week ,
  product_price_per_month,  product_deposit ,  product_pickup_address ,  product_subcategory_id ,
  product_image ,  product_image2,  product_image3, product_image1_thumbnail ,  product_display ,
  product_date_added ,  product_last_modified , product_pickup_location,  product_image2_thumbnail,
  product_image3_thumbnail,date_deleted,owner_user_id
)
SELECT  product_id,  product_name ,   product_description ,   product_category_id ,  product_status ,  product_duration,  product_price_per_day,  product_price_per_weekend,
product_price_per_week ,  product_price_per_month,  product_deposit ,  product_pickup_address,
product_subcategory_id ,  product_image , product_image2,  product_image3,  product_image1_thumbnail ,  product_display ,  product_date_added ,  product_last_modified ,
product_pickup_location,  product_image2_thumbnail,  product_image3_thumbnail, NOW(),inUserId
FROM product
WHERE product_id = inProductId;



DELETE FROM product WHERE product_id = inProductId;
 commit;


set ret = 1;
END


2. Goto Php code and
 $c = mysqli_query ($dbc, "CALL remove_product('$pid','$cur_uid',@res)");
// For debugging purposes:
  if (!$c) echo mysqli_error($dbc);

 /* TO CHECK PROCEDURE RETURN STATUS: */
$res = mysqli_query($dbc,"SELECT @res");
$row = mysqli_fetch_array($res);
$ret = (int)$row[0];

if( $ret ) { //product removed
  $dbc->close(); /* Close the connection */ 
  echo ""; 
 }
else //product not removed
 {
  echo "product not found";
 }

Σχόλια

Δημοφιλείς αναρτήσεις