0

How do I do that while using PDO? I once did it but not with PDO. Do I wrap the $attack = $query->execute(array.. in an if statement?

What is the best way to do that? And any advice on my coding skills would be appreciated :)

Here's my code

if(isset($_POST['submit'])){
    $firstname  = $_POST['firstname'];
    $middlename = $_POST['middlename'];
    $lastname   = $_POST['lastname'];
    $spousename = $_POST['spousename'];
    $bday       = $_POST['bday'];
    .
    .
    .
    .*other $_POST[''] stuffs*

$q = "INSERT into crf(col_fn, col_mn, col_ln, col_sn, col_bday, col_home, col_telno, col_mobno, col_email, col_gender, col_civilstat)
              VALUES(:fn, :mn, :ln, :sn, :bday,  :homeadd, :telno, :mobno, :email, :gender, :civilstat);";

$query = $db->prepare($q);
try{
$attack= $query->execute(array(
    ":fn"       =>  $firstname,
    ":mn"       =>  $middlename,
    ":ln"       =>  $lastname,
    ":sn"       =>  $spousename,
    ":bday"     =>  $bday,
    ":homeadd"  =>  $homeadd,
    ":telno"    =>  $telno,
    ":mobno"    =>  $mobno,
    ":email"    =>  $emailadd,
    ":gender"   =>  $gender,
    ":civilstat"=>  $civilstat
    ));

}catch(PDOException $e){
    $e->getMessage();
}

?>
Tristan
  • 3,301
  • 8
  • 22
  • 27
juju17
  • 271
  • 4
  • 15

2 Answers2

0

http://php.net/manual/en/pdo.query.php

Return Values

PDO::query() returns a PDOStatement object, or FALSE on failure.

So wrapping in an if == false would be great to check if the query has been executed

Naranca
  • 73
  • 7
0

You don't need it. Every statement is succesful which caused no error. Therefore you don't need a dedicated condition to test for success.

if(isset($_POST['submit'])){
    unset($_POST['submit'])); // get rid of extra stuff in $_POST
    $q = "INSERT into crf(col_fn, col_mn, col_ln, col_sn, col_bday, col_home, col_telno, col_mobno, col_email, col_gender, col_civilstat)
              VALUES(:firstname, :middlename, :lastname, :spousename, :bday,  :homeadd, :telno, :mobno, :email, :gender, :civilstat);";
    $db->prepare($q)->execute($_POST);
    echo "Success!"; // or whatever. 
}

this is all the code you need

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345