I have a MySQL stored routine that has an IN parameter that is of type INTEGER (IN p_user_id INTEGER). If the user is creating a new user I pass p_user_id in as ” otherwise if the user is updating a user, I pass in the user_id of the user being edited. My issue with this is that when p_user_id is coming in as ” it is getting converted to 0. I’ve dumped the user id out right before PHP sends the value to MySQL (and the value is ”) and dumped it out right at the beginning of the MySQL routine and the p_user_id is now 0. Can I get some insight for how to handle this so that I can have the p_user_id IN parameter be NULL. Thanks in advance!
PHP Code:
<?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 stored routine:
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
1 Answer