我无法弄清楚为什么我的MySQL存储过程没有将数据插入到两个表中。我的脚本确实执行,但行数为0,表没有新数据。这是我的插入存储过程:
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_CreateAthlete`(IN athleteID int(11), IN athleteName varchar(100), IN athleteSurname varchar(100), IN emailAddress varchar(150), IN userName varchar(100), IN athletePassword varchar(100), IN securityQuestion varchar(200), IN securityAnswer varchar(100), IN athleteCountry varchar(100), IN dateOfBirth varchar(30), IN userType varchar(100))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
END;
DECLARE EXIT HANDLER FOR SQLWARNING
BEGIN
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO iBalekaDB.user (UserID, `Name`, Surname, EmailAddress, Country, UserType, DateOfBirth)
VALUES (athleteID, athleteName, athleteSurname, emailAddress, athleteCountry, userType, dateOfBirth);
INSERT INTO iBalekaDB.usercredential (UserName, `Password`, SecurityQuestion, UserID, SecurityAnswer)
VALUES (userName, enteredPassword, securityQuestion, athleteID, securityAnswer);
COMMIT;
END
这是应该将数据插入数据库的PHP脚本。表usercredential在UserID上有一个外键 - 这来自用户表。
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
header('Content-Type: application/json');
require_once("db_connection.php");
$athleteID = $_POST['AthleteID'];
$name = $_POST['Name'];
$surname = $_POST['Surname'];
$country = $_POST['Country'];
$dateOfBirth = $_POST['DateOfBirth'];
$emailAddress = $_POST['EmailAddress'];
$userName = $_POST['Username'];
$password = $_POST['Password'];
$securityQuestion = $_POST['SecurityQuestion'];
$securityAnswer = $_POST['SecurityAnswer'];
$type = "A";
$registrationArray;
if (isset($athleteID, $name, $surname, $country, $dateOfBirth, $emailAddress, $userName, $password, $securityQuestion, $securityAnswer)) {
$checkAthlete = $connectionObject->prepare("CALL sp_CheckAthleteRegistration(?,?)");
$checkAthlete->bindParam(1, $athleteID, PDO::PARAM_INT);
$checkAthlete->bindParam(2, $emailAddress, PDO::PARAM_STR);
$checkAthlete->execute();
if ($checkAthlete->rowCount() > 0) {
$msg = array('error' => array('message', 'The email address you tried using has been used'));
echo json_encode($msg);
$checkAthlete->closeCursor();
} else {
$checkAthlete->closeCursor();
$checkAthleteUsername = $connectionObject->prepare("CALL sp_CheckUsername(?,?)");
$checkAthleteUsername->bindParam(1, $athleteID, PDO::PARAM_INT);
$checkAthleteUsername->bindParam(2, $userName, PDO::PARAM_STR);
$checkAthleteUsername->execute();
if ($checkAthleteUsername->rowCount() > 0) {
$errMsg = array('error' => array('message', 'The username you tried registering has already been used'));
echo json_encode($errMsg);
} else {
$checkAthleteUsername->closeCursor();
$insertAthlete = $connectionObject->prepare("CALL sp_CreateAthlete(?,?,?,?,?,?,?,?,?,?,?)");
$insertAthlete->bindParam(1, $athleteID, PDO::PARAM_INT);
$insertAthlete->bindParam(2, $name, PDO::PARAM_STR);
$insertAthlete->bindParam(3, $surname, PDO::PARAM_STR);
$insertAthlete->bindParam(4, $emailAddress, PDO::PARAM_STR);
$insertAthlete->bindParam(5, $userName, PDO::PARAM_STR);
$insertAthlete->bindParam(6, $password, PDO::PARAM_STR);
$insertAthlete->bindParam(7, $securityQuestion, PDO::PARAM_STR);
$insertAthlete->bindParam(8, $securityAnswer, PDO::PARAM_STR);
$insertAthlete->bindParam(9, $country, PDO::PARAM_STR);
$insertAthlete->bindParam(10, $dateOfBirth, PDO::PARAM_STR);
$insertAthlete->bindParam(11, $type, PDO::PARAM_STR);
$insertAthlete->execute();
if ($insertAthlete->rowCount() > 0) {
$insMsg = array('success' => array('message', 'You have successfully registered for an account with iBaleka'));
echo json_encode($insMsg);
} else {
$regerrrMsg = array('error' => array('message', 'An error occurred while registering for the system. Please try again'));
echo json_encode($regerrrMsg);
}
}
}
} else {
$registrationArray = array("error" => array("message", "Please ensure all fields have data"));
}