leecarter leecarter - 18 days ago 7
PHP Question

SQLSTATE[42S22]: Column not found: 1054 Unknown column '$value' in 'field list''

try {
$db = new PDO("mysql:host=$host;dbname=$dbname",$user,$password) ;
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$formInfo = array('fname','lname','email','password','gender') ;
$entry = array() ;
foreach($formInfo as $data) {

$entry[$data] = $_POST[$data];

}
//print_r($entry);


$salt = Hash::gen_salt() ;
$sqlcollum = array(
'First_name'=> $entry['fname'] ,
'Last_name' => $entry['lname'] ,
'email' => $entry['email'] ,
'password' => Hash::gen_password($entry['password'],$salt) ,
'salt' => $salt,
'gender' => $entry['gender'] ,
'date' => date('Y-m-d H:i'),
'groups' => '1'
) ;
$sqlKeys = array_keys($sqlcollum) ;
$sqlValues = array_values($sqlcollum) ;
$keys = "'".implode("','", $sqlKeys)."'" ;
$value = "'".implode("','", $sqlValues)."'" ;
//echo $keys."<br/>" ;
//echo $value."<br/>" ;


$db->beginTransaction() ;

$insert = $db->query('INSERT INTO register ($keys) VALUES ($value)') ;

if($insert) {
echo "true" ;
} else {
$db->errorCode() ;
echo "false" ;
}
$db->commit() ;


} catch(PDOExpection $e) {
$db->rollback() ;
die($e->getMessage()) ;
}


Why do I get this error?


error::Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column '$value' in 'field list'' in register.php:46

Answer

You can try this .

 $keys = implode(",",$sqlKeys) ;

this working .