PHP MySQL:更新用户个人资料

时间:2019-03-17 16:41:50

标签: php mysql

我的登录系统中的个人资料页面出现问题。当我想更新用户的用户名和电子邮件时,我只能更新两者之一。看看我把重点放在哪里。如果我使用用户名,则只会更新用户名,电子邮件也是如此。

这是我的代码:

function updateProfile($db, $errors)
{       
    $id = $_SESSION['user']['id'];
    $username = mysqli_real_escape_string($db, $_POST['username']);
    $email = mysqli_real_escape_string($db, $_POST['email']);   
    $field_check_query = "SELECT * FROM users WHERE username='$username' email='$email'";
    $result = mysqli_query($db, $field_check_query);
    $field = mysqli_fetch_assoc($result);

    // Checks if username is already taken
    if ($field) { 
        if ($field['username'] === $username) {
          array_push($errors, "Gebruikersnaam is al reeds ingenomen");
        }       
        // Checks if email is already taken
        if ($field['email'] === $email) {
            array_push($errors, "E-mailadres is al reeds ingenomen");
        }
    }

    if (count($errors) == 0) {
        if (isset($_POST['.......'])) {
            $query = "UPDATE users SET username='$username' email='$email' WHERE id=$id";
            mysqli_query($db, $query);
            header('location: profile.php?profileeditedsuccesfully');
            die($query);
        }
    }   
}

1 个答案:

答案 0 :(得分:0)

在“选择”中将AND添加到WHERE:

$field_check_query = "SELECT * FROM users WHERE username='$username' AND email='$email'";

添加逗号以分隔更新中的配对。

=$query = "UPDATE users SET username='$username', email='$email' WHERE id=$id";