當試圖從表單中獲取數據並將其添加到名爲test的數據庫內的稱爲對象的表時,我遇到了一個小問題。嘗試通過表單插入數據庫時出錯。 SQLSTATE [42S22]
當我打開窗體上添加一些信息,然後按提交,我得到這個錯誤:
Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Mr' in 'field list' in *path**
的HTML和PHP代碼都在同一目錄下,兩個單獨的文件。
page_1.php
<form action="connect.php" method="post">
Title: <input type="text" name="title"><br />
First name: <input type="text" name="fname"><br />
About: <textarea name="about"></textarea><br />
<input type="reset" name="reset">
<input type="submit" name="submit" value="OK">
</form>
connect.php
try{
$dh = new PDO('mysql:host=127.0.0.1;dbname=test','root','');
$dh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo $e->getMessage();
die();
}
$title = $_POST['title'];
$content = $_POST['about'];
$name = $_POST['fname'];
$sql = "INSERT INTO objects (post_title, post_content, post_name, post_date)
VALUES ({$title}, {$content}, {$name})";
$query = $dh->query($sql);
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo $r->post_name, '<br>';
}
你剛剛錯過了添加日期 –
'post_date'是否有'CURDATE()'的默認約束?如果是這樣,從'INSERT INTO'中刪除'post_date' – Arulkumar