-2

I have a form with name, last name, email, and sum select an option. email its unique and you can't enter the email that's already in use by another user. the problem begins when user change just the name and without the email, I get an error the email already in use because it's in use on the current user and I need to remain this email chack to avoid duplication emails and keep it in one form

if (isset($_POST["submit"])) {

$db = new PDO('mysql:host=localhost;dbname=avihai_bid;charset=utf8', '****', '*******');
$sql = "SELECT * FROM users WHERE id = '$uid'";
$result = $db->prepare($sql);
$result->execute();
$user = $result->fetch(PDO::FETCH_ASSOC);

$nameError = '';

if (empty($_POST['name'])) {
    $nameError = 'שם הוא שדה חובה';

    $valid = false;

}

$lastnameError = '';

if (empty($_POST['lastname'])) {
    $lastnameError = 'שם משפחה הוא שדה חובה';

    $valid = false;

}

$emailError = '';

if (empty($_POST['email'])) {
    $emailError = 'אימייל הוא שדה חובה';

    $valid = false;

} elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $emailError = 'אימיל לא תקין';

    $valid = false;

} elseif ($_POST['email']) {
    $query = $db->prepare("SELECT * FROM users WHERE email = :email");
    $query->bindParam(':email', $_POST['email']);
    $query->execute();
    if($POST['email'] === $user['email']){
        $valid = true;
    } elseif ($query->rowCount() == 1) {
        $emailError = 'אימייל זה קיים כבר אצלנו במערכת';
        $valid = false;
    }
}


if ($valid) {

    $sql = "UPDATE users SET name = :name, lastname = :lastname, email = :email, build_stage = :build_stage WHERE id = '$uid'";

    $insert = $db->prepare($sql);

    $insert->bindParam(':name', $_POST['name'], PDO::PARAM_STR);

    $insert->bindParam(':lastname', $_POST['lastname'], PDO::PARAM_STR);

    $insert->bindParam(':email', $_POST['email'], PDO::PARAM_STR);

    $insert->bindParam(':build_stage', $_POST['build_stage']);

    $insert->execute();

    if ($insert->rowCount() > 0) {
        $sql = "SELECT * FROM users WHERE id = '$uid'";
        $result = $db->prepare($sql);
        $result->execute();

        if ($result->rowCount() == 1) {
            $user = $result->fetch(PDO::FETCH_ASSOC);
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['first_name'] = $user['name'];
            $_SESSION['last_name'] = $user['lastname'];
            $_SESSION['user_email'] = $user['email'];
            $_SESSION['build_stage']  = $user['build_stage'];

        }

    }
}

}

Mehran
  • 282
  • 1
  • 6
  • 17

1 Answers1

0
//just REMOVE EMAIL in update
$sql = "UPDATE users SET name = :name, lastname = :lastname, 
build_stage = :build_stage WHERE id = '$uid'";
  • While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context. – Robert Columbia Nov 25 '18 at 23:50
  • ok, but if the user want to update the email i don't want to do another form to this – Yosef Dadya Nov 26 '18 at 08:46