Reputation: 69
I want to change the error code SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '[email protected]' for key 'email' message into something like "error email already in use"
create.php
<?php
require_once 'dbconfig.php';
if ($_POST) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$contactnum = $_POST['contactnum'];
$email = $_POST['email'];
$pass = $_POST['pass'];
$lang = $_POST['lang'];
try {
$stmt = $db_con->prepare("INSERT INTO tbluser(fname,lname,contactnum,email,pass,lang) VALUES(:ufname,:ulname,:ucontact,:uemail,:upass,:ulang)");
$stmt->bindParam(":ufname", $fname);
$stmt->bindParam(":ulname", $lname);
$stmt->bindParam(":ucontact", $contactnum);
$stmt->bindParam(":uemail", $email);
$stmt->bindParam(":upass", $pass);
$stmt->bindParam(":ulang", $lang);
if ($stmt->execute()) {
echo "Successfully Added";
} else {
echo "Query Problem";
}
} catch(PDOException $e) {
echo $e->getMessage();
}
}
?>
I tried to add something like this
if (mysql_errno() == 1062) {
echo"error email already in use!";
}
But can't make it work. Thanks for your help, I'm still new in php. Can someone advise me how to use PDO?
addform.php
<style type="text/css">
#dis{
display:none;
}
</style>
<div id="dis">
<!-- here message will be displayed -->
</div>
<form method='post' id='emp-SaveForm' action="#">
<table class='table table-bordered'>
<tr>
<td>First Name</td>
<td><input type='text' name='fname' class='form-control' required /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type='text' name='lname' class='form-control' required></td>
</tr>
<tr>
<td>Contact Number</td>
<td><input type='number' name='contactnum' class='form-control' required></td>
</tr>
<tr>
<td>Email</td>
<td><input type='email' name='email' class='form-control' required /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name='pass' class='form-control' required /></td>
</tr>
<tr>
<td>Language</td>
<td><input type='text' name='lang' class='form-control' required /></td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
<span class="glyphicon glyphicon-plus"></span> Save this User
</button>
</td>
</tr>
</table>
</form>
Upvotes: 4
Views: 92
Reputation: 1414
Errors can show up in event/error logs and cause unnecessary noise if you are ever trying to find a legit error, so if you can I would avoid relying on them. Instead try:
IF (SELECT 1 = 1 FROM users WHERE email=?) THEN
BEGIN
SELECT 0 AS Result--signals already exists to the application
END;
ELSE
BEGIN
INSERT INTO users(columns) VALUES(values);
SELECT LAST_INSERT_ID() AS Result;
END;
END IF;
Or something along those lines.
Upvotes: 1
Reputation: 311458
mysql_errno
is part of the deprecated mysql extension - you're using PDO, so you need to check the error with it too:
if ($stmt->execute()) {
echo "Successfully Added";
} else {
if ($stmt->errorCode() == 1062) {
# Here ^
echo "error email already in use!";
} else {
echo "some other problem...";
}
}
Upvotes: 2