2012-08-23 25 views
0

林即将创建一个简单的代码添加新闻...(我的小项目) 当我点击提交我得到一个成功的文本和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>"; 
}?> 
+1

它可能不会帮助回答你的问题,但你应该停止使用'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

+2

尝试检查'mysql_query()'的返回值 - '$ result'中的实际内容是什么?尝试回显你的SQL并直接在数据库中运行它。 – andrewsi

+1

http://www.phptherightway.com/#error_reporting – Xeoncross

回答

0

你的代码有点未完成。

<?php 

// connect to the database 
include('connect_db.php'); 


if(isset($_POST['submit'])) 

{ 

    // htmlspecialchars is needed when displaying HTML to the user from an input, not for inserting into a database. mysql_real_escape_string is plenty for this purpose. 

    $title = mysql_real_escape_string($_POST['title']); 
    $text = mysql_real_escape_string($_POST['text']); 

    if ($title == '' || $text == '') 

    { 

     // You have generated an error but you are not displaying it anywhere. 

     // generate error message 
     echo 'ERROR: Please fill in all required fields!'; 

     // You will want to either send the error in a query string to this page again and display it above the form or re-echo the form here. 

    }else{ 

     // Don't submit the data if there is an error. 

     // ID should be auto-increment in your database, don't set it here even if you set it NULL, you can also have MySQL apply the current time rather than here. 

     $result = mysql_query("INSERT INTO news (`title`, `text`) 
     VALUES ('$title','$text')"); 

     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>"; 

} 
?> 
+0

Kolink击败了我的职位:) – JohnDevelops

+0

thx球员,现在得到它! - thx解释我做错了什么! –

3

NULL是一个关键字,它不应该在引号中。

同样,字段名称应该包含在反引号`中,而不是单引号',并且您还应该在其中包含表名以保持一致性。

此外,它看起来像id设置为AUTO_INCREMENT,因此您不需要将其设置为NULL。如果dateTIMESTAMP(它应该是),那么你也可以设置DEFAULT CURRENT_TIMESTAMP并从查询中删除它。

$result = mysql_query("INSERT INTO `news` (`title`,`text`) VALUES ('$title','$text')"); 
+0

伙计们, 错误:查询是空的......? 我按错误顺序做了什么? –