林即将创建一个简单的代码添加新闻...(我的小项目) 当我点击提交我得到一个成功的文本和rediret返回到页面但没有添加数据,如果我将表单留空 - 错误消息不会出现。需要帮助找到错误,没有什么添加到数据库
...是mysql_real_escape_string会救我一些麻烦?
<?php
include('connect_db.php');
if(isset($_POST['submit']))
{
$title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
$text = mysql_real_escape_string(htmlspecialchars($_POST['text']));
if ($title == '' || $text == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
}
$result = mysql_query("INSERT INTO news ('id', 'date', 'title', 'text')
VALUES ('NULL', NOW(),'$title','$text')",$conn);
echo "<b>Thank you!<br>You'll be redirected in (4) secs...";
echo "<meta http-equiv=Refresh content=4;url=add.php>";
} else {
echo "<form method='post' action='add.php'>
<legend>Add news</legend>
<label>Title</label>
<input type='text' name='title'>
<label>Text</label>
<textarea rows='5' name='text'></textarea>
<br />
<button type='submit' name='submit' class='btn'>Submit</button>
</form>";
}?>
它可能不会帮助回答你的问题,但你应该停止使用'mysql_ *'函数。他们正在被弃用。请使用[PDO](http://php.net/manual/en/book.pdo.php)(自PHP 5.1起支持)或[mysqli](http://php.net/manual/en/book)。 mysqli.php)(自PHP 4.1起支持)。如果你不确定使用哪一个,[阅读本文](http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/)。 – Matt
尝试检查'mysql_query()'的返回值 - '$ result'中的实际内容是什么?尝试回显你的SQL并直接在数据库中运行它。 – andrewsi
http://www.phptherightway.com/#error_reporting – Xeoncross