我即将创建一个简单的代码来添加新闻......(我的小项目)当我点击提交时,我得到一个成功文本,并重定向回页面,但没有添加数据,如果我将表单留空 -不出现错误消息。
...而且 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>";
}?>