user3186716
user3186716

Reputation: 37

PDO prepared statement with placeholder not work

i have some problems with execute a PDO. No error, just nothing happens. The code looks like:

$stmt = $dbh->prepare("INSERT INTO `member_accounts` ('firstname','lastname','email','password') VALUES (:fname,:lname,:e,:pw)");
                 $arr = array(
                    ':fname' => $_POST['firstname'],
                    ':lname' => $_POST['lastname'],
                    ':e' => $_POST['email'],
                    ':pw' => $_POST['password'],
                    );
                 $stmt->execute($arr);

Anyone see the problem? I'm to new at PDO.. Thanks

Upvotes: 0

Views: 663

Answers (2)

alok.kumar
alok.kumar

Reputation: 380

use the following code

        $sql="INSERT INTO `member_accounts`   
                     (firstname,lastname,email,password)  VALUES    
                      (:fname,:lname,:e,:pw)";
        $stmt = $dbh->prepare($sql);
          //pdo $stmt is false if any error occur
          if($stmt)
         {

             $arr = array(
                ':fname' => $_POST['firstname'],
                ':lname' => $_POST['lastname'],
                ':e' => $_POST['email'],
                ':pw' => $_POST['password'],
                );
             $stmt->execute($arr);

         }

Upvotes: 0

chanchal118
chanchal118

Reputation: 3647

Remove single quotes here

('firstname','lastname','email','password')

Right statement will be

$stmt = $dbh->prepare("INSERT INTO `member_accounts` (firstname,lastname,email,password) VALUES (:fname,:lname,:e,:pw)");

You can also use backtick(`) with column name but not single quotes.

$stmt = $dbh->prepare("INSERT INTO `member_accounts` (`firstname`,`lastname`,`email`,`password`) VALUES (:fname,:lname,:e,:pw)");

Upvotes: 1

Related Questions