Home > Mobile >  Transaction within a stored procedure always failing
Transaction within a stored procedure always failing

Time:03-08

I've created a stored procedure via phpmyadmin which carries out a transaction as follows:

BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
        BEGIN
            SELECT -1;
            ROLLBACK;
        END;
    START TRANSACTION;
      INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment) VALUES (name, gender, house_id, date_from,date_to, clean_rating,comments);
      
      UPDATE rental SET reviewed = 1 WHERE renter_id = renter_id AND house_id = house_id AND date_from = dateFrom AND date_to = dateTo ;
    COMMIT;
    
    SELECT 1;

END

The parameters were set like this: parameters to the stored procedure

Each time I execute the stored procedure with valid values via phpmyadmin, I get -1 returned and no tables are affected. Not quite sure what's wrong here.

Edit: Added some info

SHOW CREATE PROCEDURE sp_save_review:

| sp_save_review | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_save_review`(IN `name` VARCHAR(200), IN `gender` VARCHAR(50), IN `house_id` INT(11), IN `date_from` DATE, IN `date_to` DATE, IN `clean_rating` FLOAT, IN `comments` VARCHAR(1000), IN `renter_id` INT(11))
BEGIN
        DECLARE EXIT HANDLER FOR SQLEXCEPTION
        BEGIN
                SELECT -1;
                ROLLBACK;
        END;
        START TRANSACTION;
          INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment) VALUES (name, gender, house_id, date_from,date_to, clean_rating,comments);

      UPDATE rental SET reviewed = 1 WHERE renter_id = renter_id AND house_id = house_id AND date_from = dateFrom AND date_to = dateTo ;
    COMMIT;

    SELECT 1;

SHOW CREATE TABLE review:

| review | CREATE TABLE `review` (
  `review_id` int(11) NOT NULL AUTO_INCREMENT,
  `reviewer_name` varchar(200) CHARACTER SET utf32 NOT NULL,
  `reviewer_gender` varchar(50) CHARACTER SET utf32 NOT NULL,
  `house_id` int(11) NOT NULL,
  `rental_date_from` date NOT NULL,
  `rental_date_to` date NOT NULL,
  `house_rating` float NOT NULL,
  `house_comment` varchar(1000) CHARACTER SET utf32 NOT NULL,
  `flagged` tinyint(1) NOT NULL DEFAULT 0,
  `banned` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`review_id`),
  KEY `house_id` (`house_id`),
  CONSTRAINT `review_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `rental` (`house_id`)
) ENGINE=InnoDB AUTO_INCREMENT=70 DEFAULT CHARSET=latin1 COLLATE=latin1_bin |

SHOW CREATE TABLE rental:

| rental | CREATE TABLE `rental` (
  `renter_id` int(11) NOT NULL,
  `house_id` int(11) NOT NULL,
  `date_from` date NOT NULL,
  `date_to` date NOT NULL,
  `price` double NOT NULL,
  `reviewed` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`renter_id`,`house_id`,`date_from`),
  KEY `house_id` (`house_id`),
  CONSTRAINT `rental_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `house` (`house_id`),
  CONSTRAINT `rental_ibfk_2` FOREIGN KEY (`renter_id`) REFERENCES `renter` (`renter_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin |

CodePudding user response:

You seem to have got into a fankle with your naming try this

 DROP TABLE IF EXISTS REVIEW;
 DROP TABLE IF EXISTS RENTAL;
 
 
 CREATE TABLE `rental` (
  `renter_id` int(11) NOT NULL,
  `house_id` int(11) NOT NULL,
  `date_from` date NOT NULL,
  `date_to` date NOT NULL,
  `price` double NOT NULL,
  `reviewed` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`renter_id`,`house_id`,`date_from`),
  KEY `house_id` (`house_id`) #,
  #CONSTRAINT `rental_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `house` (`house_id`),
  #CONSTRAINT `rental_ibfk_2` FOREIGN KEY (`renter_id`) REFERENCES `renter` (`renter_id`)
) ;
 
 
 CREATE TABLE `review` (
  `review_id` int(11) NOT NULL AUTO_INCREMENT,
  `reviewer_name` varchar(200) CHARACTER SET utf32 NOT NULL,
  `reviewer_gender` varchar(50) CHARACTER SET utf32 NOT NULL,
  `house_id` int(11) NOT NULL,
  `rental_date_from` date NOT NULL,
  `rental_date_to` date NOT NULL,
  `house_rating` float NOT NULL,
  `house_comment` varchar(1000) CHARACTER SET utf32 NOT NULL,
  `flagged` tinyint(1) NOT NULL DEFAULT 0,
  `banned` tinyint(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`review_id`),
  KEY `house_id` (`house_id`),
  CONSTRAINT `review_ibfk_1` FOREIGN KEY (`house_id`) REFERENCES `rental` (`house_id`)
) ;

DROP PROCEDURE IF EXISTS P;
DELIMITER $$
CREATE PROCEDURE  P(IN `Pname` VARCHAR(200), IN `Pgender` VARCHAR(50), IN `Phouse_id` INT(11), 
                          IN `Pdate_from` DATE, IN `Pdate_to` DATE, IN `Pclean_rating` FLOAT, 
                          IN `Pcomments` VARCHAR(1000), IN `Prenter_id` INT(11))
BEGIN
        DECLARE EXIT HANDLER FOR SQLEXCEPTION
        BEGIN
                SELECT -1;
                ROLLBACK;
        END;
        START TRANSACTION;
          INSERT INTO review(reviewer_name, reviewer_gender, house_id, rental_date_from, rental_date_to, house_rating,house_comment) 
             VALUES (Pname, Pgender, Phouse_id, Pdate_from,Pdate_to, Pclean_rating,Pcomments);

          UPDATE rental 
              SET reviewed = 1 
              WHERE renter_id = Prenter_id AND house_id = Phouse_id AND date_from = Pdate_From AND date_to = Pdate_To ;
        COMMIT;

    SELECT 1;
END $$

DELIMITER ;

INSERT INTO RENTAL( `renter_id`, `house_id` ,  `date_from` ,  `date_to` ,  `price`,  `reviewed` )
VALUES (1,1,'2022-01-01','2022-01-01',10,0);

CALL P('AAA','F',1,'2022-01-01','2022-01-01',1,'BBB',1);
  • Related