So I am trying to build a function (modifying another one) and I have the array posting fine to the function. It seems that the formating may be off for the INSERT command with my function. I have been looking for the past two hrs and can't find where I may have gone run.
Here is the function, Error code below!
public static function addCompany($toInsert = array()){
self::construct();
if( count($toInsert) == 0 ){
echo "Nothing posted!";
}else {
$keys = array_keys($toInsert);
$columns = implode(",", $keys);
$colVals = implode(",:", $keys);
$sql = self::$dbh->prepare("INSERT INTO companys {$columns} VALUES(:$colVals)");
//$sql->bindValue(":id", $company);
foreach($toInsert as $key => $value){
$value = htmlspecialchars($value);
$sql->bindValue(":$key", $value);
}
$sql->execute();
return true;
}
}
Here is the array coming in.
$vname = $_POST["name"];
$vlogo = $_POST["logo"];
$vinfo = $_POST["info"];
$vsite = $_POST["site"];
$vest = $_POST["est"];
$data = array('name' => $vname, 'logo' => $vlogo, 'info' => $vinfo, 'site' => $vsite, 'est' => $vest);
Here is the error.
2017/01/07 18:30:22 [error] 9682#9682: *3352 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'name,logo,info,site,est VALUES('test','test','test','test','test')' at line 1' in /var/www/xxxx/inc/xxxxxxx.php:938
Any help would be fantastic!
$sql = "INSERT INTO companys {$columns} VALUES(:$colVals)"; echo $sql;that should show you what a mess you are making of thishtmlspecialcharson your values? This just mangles them for no reason.