I am trying to get error message or code for my sql query i currently have my method as this in a class file
public function createNew()
{
$query = "INSERT INTO " . $this->table_name . " SET userId = ?, email = ?, data1 = ?";
$obj = $this->conn->prepare($query);
$this->userId = htmlspecialchars(strip_tags($this->userId));
$this->email = htmlspecialchars(strip_tags($this->email));
$this->data1 = htmlspecialchars(strip_tags($this->data1));
$obj->bind_param("sss", $this-> userId, $this->email, $this->data1);
if ($obj->execute()) {return true;}
return false;
}
And here is the php file
if ($_SERVER['REQUEST_METHOD'] === "POST") {
$data = json_decode(file_get_contents("php://input"));
$users->userId = $data->userId;
$users->email = $data->email;
$users->data1 = $data->data1;
if ($users->createNew()) {
http_response_code(200);
echo json_encode(array(
"status" => 200,
"message" => "Success"
));
} else {
http_response_code(200);
echo json_encode(array(
"status" => 500,
"message" => "Failed"
));
}
}
The code currently work fine but when a duplicate value is insert for email or userId i get error message as
"message" => "Failed"
but what i want is the specific error that is when userId is duplicated the error message should be userId already exists or if it the email then the message should be email already exists instead of all the error returning Failed. Thanks