I have a three field.
First Field is auto numbering.(PK)
Second Field is user id.
Last Field is user pw.
Anyway, This is my code. This page called by JSON. This page can call multiple user. so it should ensure conflict id. if id is conflict, id should be auto numbering to max.
<?php
$conn=mssql_connect('test_database', 'root', '1357');
if (!$conn) {
return -1;
}
header("Content-Type:application/json");
$id=($_POST['id']);
$userid=($_POST['userid']);
$userpw=($_POST['userpw']);
$sql="INSERT INTO dbo.testTable
(id
,userid
,userpw)
VALUES
('$id'
,'$userid'
,'$userpw')";
function customError($errno,$errstr)
{
if (strpos($errstr,"Violation of PRIMARY KEY")!==false){
$query_id="
SELECT id
FROM testTable
order by LEN(id) DESC, id DESC
";
$result_id=mssql_query($query_id,$conn);
$id=mssql_result($result_id,0,0)+1;
$result= mssql_query($sql,$conn);
echo json_encode($result);
}
else{
echo $errstr;
die();
}
}
set_error_handler("customError");
$result= mssql_query($sql,$conn);
echo json_encode($result);
?>
I Try control error message 'Violation of PRIMARY KEY" by using set_error_handler.
But $id,$userid,$userpw,$sql can not use in set_error_handler function.
Notice: Undefined variable: sql