请帮帮我。我正在尝试将记录插入2个表中,我可以在第一个表中插入,但对于第二个表则不能。
我有3个存储过程
第一个存储过程似乎还可以,因为我可以在第一个表中获取输出,但在第二个表中却无法获取最后插入的ID。
第一个存储过程
DELIMITER $$
CREATE DEFINER=`kno`@`localhost` PROCEDURE `HotelInsert`(IN `ZIP` BIGINT, IN `Ammn` VARCHAR(255), IN `HotelName` VARCHAR(255), IN `Add1` VARCHAR(255), IN `Add2` VARCHAR(255), IN `Det` VARCHAR(255), IN `ContactPer` VARCHAR(255), IN `Contact` VARCHAR(255), IN `CEmail` VARCHAR(255), IN `Image` VARCHAR(255), IN `StarCateg` TINYINT)
NO SQL
DETERMINISTIC
INSERT INTO Accommodation(
AccommodationId,
AccommodationTypeId,
ZipId,
PackageId,
Amenities,
Name,
AddressOne,
AddressTwo,
AccommodationStatus,
Details,
ContactPerson,
StarCategory,
ImageGallery,
ContactPhone,
ContactEmail
)
VALUES(
NULL,
1,
ZIP,
4,
Ammn,
HotelName,
Add1,
Add2,
1,
Det,
ContactPer,
StarCateg,
Image,
Contact,
CEmail
)$$
DELIMITER ;
第二个存储过程:
DELIMITER $$
CREATE DEFINER=`kno`@`localhost` PROCEDURE `GetAccomodationIdByName`(IN `AccommodationName` VARCHAR(255), OUT `AccId` INT)
NO SQL
DETERMINISTIC
SELECT AccommodationId into AccId from Accommodation where Name=AccommodationName$$
DELIMITER ;
第三存储过程:
DELIMITER $$
CREATE DEFINER=`kno`@`localhost` PROCEDURE `AvailabilityInsert`(IN `AccId` INT, IN `RTypeId` INT, IN `AvailableRooms` INT, IN `Charges` BIGINT, IN `AvailDate` DATE)
NO SQL
INSERT INTO AccommodationAvailabilty (AvailableId, AccommodationId, RoomTypeId, Available, Price, AvailableDate) VALUES (NULL, AccId, RTypeId, AvailableRooms, Charges, AvailDate)$$
DELIMITER ;
PHP代码:
$sql = $db->prepare('CALL HotelInsert(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
$sql->bind_param('dssssssssss', $ZipId, $Amenities, $Name, $AddressOne, $AddressTwo, $Details, $ContactPerson, $ContactPhone, $ContactEmail , $target_path, $StarCategory);
$sql->execute();
$select = $db->query('SELECT @ZIP,@Ammn, @HotelName, @Add1, @Add2, @Det, @ContactPer,@Contact,@CEmail,@Image, @StarCateg');
$result = $select->fetch_assoc();
$ZipId = $result['@ZIP'];
$Amenities = $result['@Ammn'];
$AccommodationName = $result['@HotelName'];
$AddressOne = $result['@Add1'];
$AddressTwo = $result['@Add2'];
$Details = $result['@Det'];
$ContactPerson = $result['@ContactPer'];
$ContactPhone = $result['@Contact'];
$ContactEmail = $result['@CEmail'];
$target_path = $result['@Image'];
$StarCategory = $result['@StarCateg'];
//Hotel Calling AccommodationId by AccommodationName with GetAccomodationIdByName(); stored procedure.
$sql = $db->prepare('CALL GetAccomodationIdByName(?)');
$sql->bind_param('s', $AccommodationName);
$sql->execute();
$select = $db->query('SELECT @AccId');
$result = $select->fetch_assoc();
$AccommodationId = $result['@AccId'];
//Hotel Insert RoomType by calling AvailabilityInsert(); Store procedure.
$sql = $db->prepare('CALL AvailabilityInsert(?,?,?,?,?)');
$sql->bind_param('iiids', $AccommodationId, $RoomTypeId, $Available, $Price, $AvailableDate);
$sql->execute();
$select = $db->query('SELECT @AccId,@RTypeId, @AvailableRooms, @Charges, @AvailDate');
$result = $select->fetch_assoc();
$AccommodationId = $result['@AccId'];
$RoomTypeId = $result['@RTypeId'];
$Available = $result['@AvailableRooms'];
$Price = $result['@Charges'];
$AvailableDate = $result['@AvailDate'];
header("Location: ../hotel-all.php?id=$Name");
exit();