我試圖製作一個新聞系統,所以我只需填寫表單以發佈文章。不幸的是,似乎在多次嘗試和方法之後,我的INSERT INTO查詢似乎沒有做任何事情。 我使用2個單獨的文件:addnews.php(表單)和confirm.php。 我翻譯了法語的所有內容,因此讀起來更容易。我的INSERT INTO查詢不起作用
這裏是一個做工精細的表單代碼:
<div id="centerbox" class="news1" align="center">
<h1>Write a news</h1><br>
<form method="post" action="confirm.php">
<p>Title:</p>
<input type="text" name="title"><br>
<p>Content:</p>
<textarea name="content" rows="8" cols="45"></textarea>
<br>
<p>Picture to display:</p>
<select name="picture">
<option value="news">pics/INFO_IMPORTANTE.jpg</option>
<option value="event">pics/EVENEMENT.jpg</option>
<option value="helpme">pics/DEMANDE_AIDE.jpg</option>
<option value="helpyou">pics/PROPOSITION_AIDE.jpg</option>
</select>
<p>Your name:</p>
<input type="text" name="author"><br>
<input type="hidden" name="date" value="<?php echo date("d/m/Y"); ?>">
<input type="submit" value="Confirm">
</form>
</div>
這裏是在confirm.php代碼:
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=L1-EVMAN', 'root', '');
}
catch (Exception $e)
{
die('Error : ' . $e->getMessage());
}
?>
<?php
$title = htmlspecialchars($_POST['title']);
$picture = $_POST['picture'];
$content = htmlspecialchars($_POST['content']);
$author = htmlspecialchars($_POST['author']);
$time = date('d/m/Y');
if (empty($title) OR empty($content) OR empty($author))
{
echo '<font color="red">You have to fill everything !</font><br>
<a href="http://localhost/sourcemaster/addnews>Start over</a>';
}
else
{
$addnews = $bdd->prepare('INSERT INTO news (id, title, pic, content, author, time) VALUES(NULL, :$title, :$picture, :$content, :$author, :$time)');
$addnews->execute(array(
'title' => $title,
'pic' => $picture,
'content' => $content,
'author' => $author,
'time' => $time
));
echo 'The news has been added correctly.';
}
?>
的問題是,當我填寫表格並確認,它確實說新聞已被添加,但沒有任何內容出現在新聞頁面上(當我手動添加表格中的所有內容時,它可以正常工作)。 我真的很喜歡這一切,我可以告訴你,我在最後完成了一些簡單的練習,遵循了一些我發現的教程,但沒有一個能夠幫助我。
所以我問你們的幫助。隨意要求更多細節或任何事情。
非常感謝!
你需要設置PDO連接屬性火例外的一件事。 '$ bdd-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);' – Drew
這不是你如何在sql中使用佔位符 - 它應該更像'INSERT INTO news(id,title,pic,content,作者,時間)VALUES(NULL,:title,:picture,:content,:author,:time)'和params數組中的相應條目(執行語句的地方)應該有冒號 – RamRaider
刪除您的美元符號綁定參數。即在實際的插入查詢中,將$ title改爲:title。 –