我有这段代码
我试图更新用户并先检查它是否已经存在。 而我的问题是它也将自己检查为现有用户。
如何防止在PDO中更新之前将自身检查为现有数据?
try
{
$stmt = $crud->runQuery("SELECT user_name, user_email FROM tbl_login WHERE user_name=:user_name OR user_email=:user_email");
$stmt->execute(array(':user_name'=>$user_name, ':user_email'=>$user_email));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
if($row['user_name']==$user_name)
{
$msg = "<div class='alert alert-danger'>
<strong>Error!</strong> Username is already taken!
</div>";
}
else if($row['user_email']==$user_email)
{
$msg = "<div class='alert alert-danger'>
<strong>Error!</strong> Email is already taken!
</div>";
}
else
{
if($crud->update($edit_id,$user_name,$user_email))
{
$msg = "<div class='alert alert-info'>
<strong>Great!</strong> Record was updated successfully <a href='index.php'>Dashboard</a>!
</div>";
}
else
{
$msg = "<div class='alert alert-warning'>
<strong>SORRY!</strong> ERROR while updating record !
</div>";
}
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
答案 0 :(得分:1)
你想象的很容易。我不会给你确切的代码,但我会给你逻辑。您不必通过1检查数据条目。我知道您已经知道了。
古德勒克!