具有“IF”条件的MySQL“UPDATE”语句

时间:2016-02-12 04:38:20

标签: php

我正在尝试创建一个UPDATE条件的IF语句。

如果电子邮件和用户名不重复或在数据库中找不到,我想更新用户的详细信息。

我坚持使用这段代码:

<?php

include "connection.php";
$user   = $_REQUEST['user'];
$em     = $_REQUEST['email'];
$id     = $_REQUEST['id_user'];

//Check Email in Database
$query = mysqli_query($con,"SELECT username, email from `user` where id_user = '$id'");
$result = mysqli_fetch_object($query);

if (strtolower(trim($query->email)) == strtolower(trim($em))
    || strtolower(trim($user->username)) == strtolower(trim($user))
) {
    //next to condition  username
} else {
    $data_email = mysqli_query($con,"select email from user where em='".$em."'");
    $total_email = mysqli_num_rows($data_email);

    if($total_email > 0) {
        echo "Email Not Available";
    }  else {
        //next to condition username
    }
}

//Check Username in Database                        
$data_us_user = mysqli_query($con,"select username from user where id_user='".$id."'");
$us_user = mysqli_fetch_object($data_us_user);
if (strtolower(trim($us_user->username))==strtolower(trim($user))) {
    //next to query update
} else {
    $data_username = mysqli_query($con,"select username from user where username='".$em."'");
    $total_username = mysqli_num_rows($data_username);
    if($total_username > 0) {
        echo "Username Not Available";
    } else {
        //next to query update
    }
} else {
    //Finally Query Update
    mysqli_query($con,"update user set username='".$user."',em='".$em."' where id_user='".$id."' ");
    echo "OK";
}

3 个答案:

答案 0 :(得分:2)

以下PHP脚本将检查

  • 如果用户的更新电子邮件和更新的用户名相同
  • 如果用户的新电子邮件和用户名已被其他用户使用
  • 如果两个条件都不符合,用户的详细信息将会更新

PHP代码:

<?php

include "connection.php";
$user   = $_REQUEST['user'];
$em     = $_REQUEST['email'];
$id     = $_REQUEST['id_user'];

//Getting user' details in database
$query = mysqli_query($con, "SELECT username, email from `user` where id_user = '$id'");
$result = mysqli_query($query);

//Query to find if email exists
$query2 = mysqli_query($con,"SELECT `email` from `user` WHERE em = '$em'");
$result2 = mysqli_num_rows($query2);

//Query to find if username exists
$query3 = mysqli_query($con,"SELECT `email` from `user` WHERE id_user = '$user");
$result3 = mysqli_num_rows($query3);

while ($row = mysqli_fetch_row($result)){
    list($userfromdb, $emfromdb) = $row;
}

if (strtolower(trim($userfromdb)) == strtolower(trim($user))){

    //will return true if user's username is the same before updating
    echo 'Username cannot be the same!';

} else if(strtolower(trim($emfromdb)) == strtolower(trim($em))) {

    //will return true if user's email is the same before updating
    echo 'Email cannot be the same!';

} else if($result2 > 0) {

    echo "Email Not Available";

} else if($result3 > 0) {

    echo "Username Not Available";

} else {

    //Finally Query Update
    mysqli_query($con, "UPDATE `user` set username = '$user',em = '$em' WHERE id_user = '$id'");

    //check if row updated successfully
    $result4 = mysqli_affected_rows($con);

    if ($result4 > 0) {
        echo "Updated details successfully";
    } else {
        echo "An error occurred while updated details.";
    }

}

?>

这应该有效,谢谢!

答案 1 :(得分:0)

$data_user= mysqli_query($con,"select email,username from user where id_user='".$id."'");

$userdetail = mysqli_fetch_object($data_user);
if (strtolower(trim($userdetail->email))==strtolower(trim($em)) || strtolower(trim($userdetail->username))==strtolower(trim($user)))
     {
         return "error message";
     }else{
        //your update method here
    }

答案 2 :(得分:0)

  

如果不关心指定列相关消息,

<?php
include "connection.php";
$user   = $_REQUEST['user'];
$em     = $_REQUEST['email'];
$id     = $_REQUEST['id_user'];
$data_us_em      = mysqli_query($con,"select count(email) as count from user where id_user='".$id."' OR em='".$em."' OR  username='".$em."'");
if($data_us_em)
{
    $us_em           = mysqli_fetch_assoc($data_us_em);
    $count=$us_em['count'];
    if($count)
    {
        echo 'Can not update';
    }else
    {
        mysqli_query($con,"update user set username='".$user."',em='".$em."' where id_user='".$id."' ");
        echo "OK";
    }
}
?>