0

I'm trying to convert mysql_query over to prepared statements, but it's failing silently and I'm not sure where I'm going wrong. Here's my proc.php page for a form:

$db = new PDO('mysql:host=XXX;dbname=XXX;charset=utf8', 'XXX', 'XXX');

if ($_POST['submit']) {
    $type = $_POST['type'];
    $auth1_lname = trim($_POST['auth1_lname']);
    $auth1_fname = trim($_POST['auth1_fname']);
    $today =  date("Y-m-d"); 

$stmt = $db->prepare("INSERT INTO table_base ( type , publ_date , auth1_lname , auth1_fname ) 
        VALUES (:type, :today, :auth1_lname , :auth1_fname) ");
    $stmt->bindParam(':type', $type);
    $stmt->bindParam(':today', $today);
    $stmt->bindParam(':auth1_lname', $auth1_lname);
    $stmt->bindParam(':auth1_fname', $auth1_fname);
    $stmt->execute();

    $bid = $db->lastInsertId();

    $subj_area = $_POST['subj_area'];
    $subject = 'subj_area';
    $subjs = '';

$stmt = $db->prepare("INSERT INTO table_meta (bid, key, value) VALUES (:bid, :key, :value)");
    $stmt->bindParam(':bid', $bid);
    $stmt->bindParam(':key', $subject);
    $stmt->bindParam(':value', $subjs, PDO::PARAM_STR);
    foreach($subj_area as $subjs) {
       $stmt->execute();
    }

    $geo_area = $_POST['geo_area'];
    $geograph = 'geo_area';
    $geos = '';

$stmt = $db->prepare("INSERT INTO table_meta (bid, key, value) VALUES (:bid, :key, :value)");
    $stmt->bindParam(':bid', $bid);
    $stmt->bindParam(':key', $geograph);
    $stmt->bindParam(':value', $geos, PDO::PARAM_STR);
    foreach($geo_area as $geos) {
       $stmt->execute();
    }
}   
  1. I'm not sure I'm even doing this right.
  2. I see comments elsewhere on SO that your PHP must be this tall to use PDO, but php.net's page on PDO doesn't list PHP requirements. Am I failing b/c my PHP5 host doesn't have the right drivers?
  3. Is there a way to add a die(mysql_error()) so at least it wouldn't be a silent failure?
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
kl02
  • 580
  • 6
  • 24
  • See example 1 http://www.php.net/manual/en/pdo.construct.php – vascowhite Jul 07 '13 at 20:51
  • Made those changes (thanks!) -- no errors, but still nothing being added to the tables. So at least I've got connection, which means the issue must be somewhere in the $stmt parts. – kl02 Jul 07 '13 at 21:09
  • its common to configure pdo to throw exceptions when errors happen. otherwise, explicitly configure it not to throw, andf then use the PDO::errorCode(), PDO::errorInfo(), PDOStatement::errorCode() and PDOStatement::errorInfo() methods. – goat Jul 07 '13 at 21:30
  • 1
    [PDO query fails but I can't see any errors. How to get an error message from PDO?](http://stackoverflow.com/a/15990858/285587) – Your Common Sense Jul 08 '13 at 05:47

0 Answers0