try {
$db = new PDO("mysql:host=$host;dbname=$dbname",$user,$password) ;
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlcollum = array(
'First_name'=> 'gaurav',
'Last_name' => 'sundaram',
) ;
$Keys = array_keys($sqlcollum) ;
$Valuess = array_values($sqlcollum) ;
$Valuess = "'".implode("'", $Valuess)."'" ;
$db->beginTransaction() ;
$insert = $db->prepare('INSERT INTO register ($Keys) VALUES ($Valuess)') ;
$insert->execute() ;
;
if($insert) {
echo "true" ;
} else {
$db->errorCode() ;
echo "false" ;
}
$db->commit() ;
} catch(PDOExpection $e) {
$db->rollback() ;
die($e->getMessage()) ;
}
錯誤是SQLSTATE [42S22]:未找到列:1054未知列'$ Valuess'in'field list ''在try2.php:31。
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>
放插入查詢在雙引號(「) –
的可能的複製逗號崩盤:HTTP://計算器。 com/questions/34630140/sqlstate42s22-column-not-found-1054-unknown-column-value-in-field-list/34630192#34630192 – DonCallisto
@DonCallisto沒有正確的答案 – gaurav