Home » Javascript » PHP – Update command with DB mysql

PHP – Update command with DB mysql

Posted by: admin August 19, 2018 Leave a comment

Questions:

I’ve tried to do this, but I just can’t find the error. It worked before where I just had “name, email, mobile” when I add “password” it stops working…

I don’t know what to do for it to work properly as it worked before, please help

if ( !empty($_POST)) {
    // keep track validation errors
    $nameError = null;
    $emailError = null;
    $mobileError = null;
    $passwordError = null;
    // keep track post values
    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $password = $_POST['mobile'];

    // validate input
    $valid = true;
    if (empty($name)) {
        $nameError = 'Please enter Name';
        $valid = false;
    }

    if (empty($email)) {
        $emailError = 'Please enter Email Address';
        $valid = false;
    }

    if (empty($mobile)) {
        $mobileError = 'Please enter Mobile Number';
        $valid = false;
    }

    if (empty($password)) {
        $passwordError = 'Please enter Password Number';
        $valid = false;
    }
    // update data
    if ($valid) {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //ERROR IS HERE    $sql = "UPDATE customers  set name = ?, email = ?, mobile = ?, password =?, WHERE id = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($name,$email,$mobile,$password,'ADD',$id));
        Database::disconnect();
        header("Location: index.php");
    }
} else {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM customers where id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($id));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $name = $data['name'];
    $email = $data['email'];
    $mobile = $data['mobile'];
    $password = $data['password'];
    Database::disconnect();
}

?>

Answers: