首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有‘an’条件的MySQL ` `UPDATE`‘语句

具有‘an’条件的MySQL ` `UPDATE`‘语句
EN

Stack Overflow用户
提问于 2016-02-12 04:38:19
回答 3查看 125关注 0票数 1

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

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

我被这个密码困住了:

代码语言:javascript
复制
<?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";
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-02-12 04:59:14

下面的PHP脚本将检查

  • 如果用户更新的电子邮件和更新的用户名相同
  • 如果用户的新电子邮件和用户名已被其他用户使用
  • 如果这两种条件都未满足,则将更新用户的详细信息

PHP代码:

代码语言:javascript
复制
<?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.";
    }

}

?>

这个应该行的,谢谢!

票数 2
EN

Stack Overflow用户

发布于 2016-02-12 04:43:09

代码语言:javascript
复制
$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
    }
票数 0
EN

Stack Overflow用户

发布于 2016-02-12 04:51:59

如果不需要指定列相关消息,

代码语言:javascript
复制
<?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";
    }
}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35354923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档