conn.php
<?php
$conn = @ mysql_connect("localhost", "root", "root") or die("数据库链接错误");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'");
?>
add.php
<?php
include("conn.php"); if($_POST['submit']){
$sql = "insert into message(id,user,title,content,lastdate)" .
"values('','$_POST[user]','$_POST[title]','$_POST[content]',now()))";
mysql_query($sql);
echo "发表成功";
}?>  <form action="add.php" method="post"">
   用户:<input type="text" name="user" size="10"/><br/>
标题:<input type="text" name="title"/><br/>
   内容:<textarea name="content"></textarea><br/>
   <input type="submit" name="submit" value="发布留言"/>
  </form>
提示成功,但数据添加不到数据库中??什么原因?

解决方案 »

  1.   


    if($_POST['submit']){
    $sql = "insert into message(id,user,title,content,lastdate)" .
    "values('','$_POST[user]','$_POST[title]','$_POST[content]',now()))";
    mysql_query($sql);
    echo "发表成功";
    }这一段有问题...
    只要$_POST[''submit] 有过传值,那么不管mysql_query()执行成功与否,都会有输出。
    echo $sql; 
    看看你的SQL语句对不对:
    values前边是不是少空格,或者id字段是不是有限制.
      

  2.   

    echo $sql;
    输出insert into message(id,user,title,content,lastdate) values ('','zhangsan','php','手册',now()))发表成功 values前面也没少空格,id就是自增。
      

  3.   

    $result=mysql_query($sql);
    if($result)
    {
    echo 'success';
    }
    else
    {
    echo 'fail';
    }
      

  4.   

    now()是输出时间吧,怎么sql里面还是now()
      

  5.   

    代码修改如下:
    <?php
    $conn = @ mysql_connect("localhost", "root", "root") or die("数据库链接错误");
    mysql_select_db("bbs", $conn);
    mysql_query("set names utf8");
    ?>
    add.php
    <?php
    include("conn.php");if(isset($_POST['submit'])){
    $sql = "insert into message(user,title,content,lastdate)" .
    "values('".mysql_real_escape_string($_POST[user])."','".mysql_real_escape_string($_POST[title])."','".mysql_real_escape_string($_POST[content])."',now()))";
    mysql_query($sql);
    echo "发表成功";
    }?>1. id是自增长的,不能传空字符串
    2. SQL是拼字符串的话必须要对其进行转义,防止数据无法写入的情况
      

  6.   

    $sql = "insert into message(id,user,title,content,lastdate)" .
    "values(NULL,'$_POST[user]','$_POST[title]','$_POST[content]',now())";
    知道错误了,id对应的是NULL,now后面多了个括号