php - PDO Insert not working with bindParam -


i using pdo connect database , works, when user logs in, want check if user's id in row, have done in code below:

<?php require 'steamauth/steamauth.php';  if(!isset($_session['steamid'])) {  $username = "unknown"; $avatar = "defaultuser"; $accid = "unknown";  $credits = "not applicable";  $avatarsmall = "smalluser"; //for dashboard  } else {  include ('steamauth/userinfo.php');  $username = &$steamprofile['personaname']; $avatar = &$steamprofile['avatarmedium']; $accid = &$steamprofile['steamid'];  $avatarsmall = &$steamprofile['avatar']; //for dashboard  $db_user = "username"; $db_pass = "password"; $db_host = "host"; $db_name = "database name";  $db = new pdo("mysql:host=".$db_host.";db_name=".db_name, $db_user, $db_pass);      try{         $check = $db->prepare("select userid userdata userid = :accountid");         $check->bindparam(':accountid', $accid, pdo::param_int);         $check->execute();         if(!$check){             die("server error: 404check, please contact member of staff if error continues.");         }else{             if($check->rowcount() > 0) {                 $creditsquery = $db->prepare("select usercredits userdata userid = :accountid3");                 $creditsquery->bindparam(":accountid3", $accid, pdo::param_int);                 $creditsquery->execute();                 //set credits variable database column                 $credits = $creditsquery->fetch(pdo::fetch_assoc);             }else{                 $sql = $db->prepare("insert userdata (userid, usercredits) values (:accountid2, '0')");                 $sql->bindparam(':accountid2', $accid, pdo::param_int);                 $sql->execute();                 if(!$sql){                     die('server error: 404insert, please contact member of staff if error continues.');                 }             }            }     }catch(pdoexception $e){         die ("server error: 404connection, please contact member of staff if error continues.");     } }         ?> 

although, when login, doesn't seem store user's id or credits 0, , table (userdata) empty.

thanks,

matt

this wrong:

    $check->execute();     if(!$check){        ^^^^^^^ 

$check doesn't magically change boolean true/false if execute fails. prepared statement object, , therefore evaluate true.

you didn't enable exceptions in pdo, therefore runs in default "return false on failure" mode, means code should be:

$res = $check->execute(); if(!$res) {   die(...); } 

and holds true other prepare/execute blocks - script killing before ever gets insert query, because test database failure wrong.


Comments