I wrote a stored procedure (mysql)
CREATE PROCEDURE `set_SiteAttendance`(IN _UserName VARCHAR(20) CHARSET utf8, IN _EventDesc VARCHAR(250) CHARSET utf8, IN _EventTime BIGINT(20))
BEGIN
DECLARE _EventID INT;
DECLARE cursorGetID CURSOR FOR
SELECT id
FROM client_pages
WHERE name = _EventDesc
LIMIT 0, 1;
-- try to add a new record to the database
INSERT INTO client_pages (id, name) SELECT (IFNULL(MAX(id), 0) + 1), _EventDesc FROM client_pages;
-- get id from the database records
OPEN cursorGetID;
FETCH cursorGetID INTO _EventID;
-- set the data on the visit of the page in the database
INSERT INTO login_history VALUES (NULL, _UserName, _EventID, _EventTime);
END
When I call it using MySQL Workbench, it works correctly.
CALL set_SiteAttendance('MyName', 'page#1', 100);
When I call it using php, then the stored procedure aborts on the INSERT statement:
$query = "CALL set_SiteAttendance('$user_name', '$user_page', $user_ticks)";
mysql_query($query);
mysql_error() call issues
Duplicate entry 'page#1' for key 'name'
why it happens and how to fix?