Summary:
I have a function that I'm using to set a key value pair in a MySQL database with PHP, it calls a utility function that does all of the inserting. I know this utility function works (use it with other functions) but for some reason, it is failing to insert the record.
The part that is confusing is that it does NOT throw a PDO Exception, I have the proper errmode set in the connection function so it is confusing me as to why I am getting no response.
Any idea how to resolve this or get it to provide an error?
Function that takes an id and assigns a key/value to it in the database
function setCredit($id){
//sets credit bool
//table to insert
$table='accountMeta';
//key
$key="isCredit";
//key value
$value="Y";
//array that is generated to insert
$valueArray=array("id"=>$id,"key"=>$key,"value"=>$value,"date"=>mysqlTime(currentDate()));
if (isset($id)){
//run the insert if $id exists
$results=insert($valueArray,$table);
echo $results;
} else {
//fail if $id doesn't exist
$results="ERROR: Invalid Inputs";
}
return $results;}
Utility Function that does the inserting
function insert($array,$table){
//$array should be formatted like "column"=>"data",... etc
$columns="";
$values="";
//generate the insert statement
foreach ($array as $id=>$val){
$columns.=$id.",";
$columnBind.=":".$id.",";
}
//cut off some extra commas after our foreach
$columns=rtrim($columns,",");
$columnBind=rtrim($columnBind,",");
//get dat connection
$DBH=dbConn();
//prep the PDO
$sql=$DBH->prepare("Insert into $table($columns) VALUES($columnBind)");
//bind each value in the PDO
foreach ($array as $id=>$val){
$sql->bindValue(":$id",$val);
}
try{
//give'r a go
$sql->execute();
return "SUCCESS";
} catch (PDOExecption $e){
//and if she says no:
return "ERROR: Failed to Insert";
logMessage("select ".$e->getMessage());
}
}
Database Connection Function
function dbConn(){
$dbName=confLine("databaseName");
$dbUser=confLine("databaseUser");
$dbPass=confLine("databasePass");
$dbAddress=confLine("databaseAddress");
try{
$DBH=new PDO("mysql:host=$dbAddress;dbname=$dbName","$dbUser","$dbPass");
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
echo "Err connecting to DB";
logMessage("dbConn ".$e->getMessage());
exit;
}
return $DBH;
}
php.inifile should havedisplay_errors = Onanderror_reporting = E_ALLsetCredit()function? Maybe try stepping through with a debugger