一切都在我的代码中工作但是如果您要创建一个用户,即使您创建了一个具有相同电子邮件的多用户,我也会接受,我已经尝试搜索某些解决方案并将其放入我的代码中,但我仍然可以“#”;使它工作,我怎样才能使SQLSTATE [23000]:完整性约束违规:1062变为"错误电子邮件已存在" ?有可能吗?
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>
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();
}
}
?>
我刚刚在php中使用PDO我在数据库中使用PDO并不知道它是否是一件好事,感谢您的帮助,如果我可以使用或只是复制粘贴,请告诉我你的代码与否:)
答案 0 :(得分:0)
您应该编辑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'];
// it's good habit to validate email address always
// i checked here it should not be blank
if ($email != '') {
// check in database email address is already taken
$sel = $db_con->prepare("select email from tbluser where email = :email");
$sel->execute(array(':email' => $email));
$row = $sel->fetch();
// if you found any row with current entered email then send it to else
if ($sel->fetchColumn() == 0) {
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();
}
} else {
echo $email . ' already taken, try other email address';
}
} else {
echo 'Email address is blank';
}
}
?>
答案 1 :(得分:0)
您需要设置PDO错误模式。例如:
/* @var $dbConn PDO */
$dbConn = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUsername, $dbPassword);
/* set the PDO error mode to exception */
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);