我有一个MySQL存储例程,其IN参数类型为INTEGER(IN p_user_id INTEGER)。如果用户正在创建新用户,则将p_user_id作为''传递,否则如果用户正在更新用户,则传入正在编辑的用户的user_id。我的问题是,当p_user_id进入''它被转换为0.我在PHP将值发送到MySQL(并且值为'')之前将用户ID转储出来并将其转储出去在MySQL例程的开头,p_user_id现在是0.我可以了解如何处理这个问题,以便我可以让p_user_id IN参数为NULL。提前致谢!
PHP代码:
<?php
session_start();
$functionCalled = $_GET['function'];
function userMaintMerge()
{
$userMaintUserId = $_GET['userMaintUserId'];
$userMaintStep = $_GET['userMaintStep'];
$userMaintFirstName = $_GET['userMaintFirstName'];
$userMaintMI = $_GET['userMaintMI'];
$userMaintLastName = $_GET['userMaintLastName'];
$userMaintUserType = $_GET['userMaintUserType'];
$userMaintSchoolId = $_GET['userMaintSchoolId'];
$userMaintGrade = $_GET['userMaintGrade'];
$userMaintLogin = $_GET['userMaintLogin'];
$userMaintLogin = $_GET['userMaintPassword1'];
$mysqli = new mysqli($_SESSION['dbaddress'],$_SESSION['user'],$_SESSION['dbpword'],$_SESSION['database']);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if (!$SelectUser = $mysqli->query("call MergeUser('$userMaintUserId','$userMaintStep','$userMaintFirstName','$userMaintMI','$userMaintLastName','$userMaintUserType','$userMaintSchoolId','$userMaintGrade','$userMaintLogin','$userMaintPassword1',@error)"))
{
echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
}
?>
MySQL存储例程:
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeUser`(IN p_user_id INTEGER
,IN p_step VARCHAR(10)
,IN p_first_name VARCHAR(100)
,IN p_mi VARCHAR(5)
,IN p_last_name VARCHAR(100)
,IN p_user_type INTEGER
,IN p_school_id VARCHAR(25)
,IN p_grade VARCHAR(2)
,IN p_login VARCHAR(25)
,IN p_password VARCHAR(25)
,OUT p_error VARCHAR(1)
)
BEGIN
insert into rjh_log values ('',p_user_id,sysdate());
IF p_step = 'add' THEN
INSERT INTO USERS
( USER_ID
, LOGIN
, FIRST_NAME
, MI
, LAST_NAME
, USER_TYPE_ID
, GRADE
, SCHOOL_ID
, PASSWORD
, ACTIVE_FLAG
)
VALUES ( NULL
, p_login
, p_first_name
, p_mi
, p_last_name
, p_user_type
, p_grade
, p_school_id
, p_password
, 'Y'
) ;
ELSE
UPDATE USERS
SET LOGIN = p_login
, FIRST_NAME = p_first_name
, MI = p_mi
, LAST_NAME = p_last_name
, USER_TYPE_ID = p_user_type
, GRADE = p_grade
, SCHOOL_ID = p_school_id
, PASSWORD = p_school_id
WHERE USER_ID = p_user_id;
END IF;
END
答案 0 :(得分:0)
$userMaintUserId = ($_GET['userMaintUserId'] == "" ? "NULL" : $_GET['userMaintUserId']);
$userMaintStep = ($_GET['userMaintStep'] == "" ? "NULL" : $_GET['userMaintStep']);
$userMaintFirstName = ($_GET['userMaintFirstName'] == "" ? "NULL" : $_GET['userMaintFirstName']);
$userMaintMI = ($_GET['userMaintMI'] == "" ? "NULL" : $_GET['userMaintMI']);
$userMaintLastName = ($_GET['userMaintLastName'] == "" ? "NULL" : $_GET['userMaintLastName']);
$userMaintUserType = ($_GET['userMaintUserType'] == "" ? "NULL" : $_GET['userMaintUserType']);
$userMaintSchoolId = ($_GET['userMaintSchoolId'] == "" ? "NULL" : $_GET['userMaintSchoolId']);
$userMaintGrade = ($_GET['userMaintGrade'] == "" ? "NULL" : $_GET['userMaintGrade']);
$userMaintLogin = ($_GET['userMaintLogin'] == "" ? "NULL" : $_GET['userMaintLogin']);
$userMaintLogin = ($_GET['userMaintPassword1'] == "" ? "NULL" : $_GET['userMaintPassword1']);