链接数据库代码:conn.php
<?php$conn= @ mysql_connect("localhost","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" size="10" name="user" ></br>
标题:<input type="text" name="title"></br>
内容:<textarea name="content" ></textarea></br><input type="submit" name="submit" value="发布留言">
</form>显示留言代码:list.php
<?php$conn= @ mysql_connect("localhost","root","") or die("数据库连接错误");
mysql_select_db("bbs",$conn);
mysql_query("set names GBK");?><table align="center" border="2" width="600" height="60" cellpadding="1" cellspacing="10" bgcolor="#99CCFF">
<?
$sql="select title from message";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){?><tr bgcolor="#FFFFCC">
<td>标题:<?=$row[title]?></td><td>用户名:<?=$row[user]?></td>
</tr>
<tr bgcolor="#66CCFF">
<td>内容:<?=$row[content]?></td>
</tr>
<tr width="800" bgcolor="#3399CC"><td></td></tr>
<?
}
?>
</table>

解决方案 »

  1.   

    add.php
    <?php
    include("conn.php");if($_POST['submit']){
      $sql="insert into message(`id`,`user`,`title`,`content`,`lastdate`) values(NULL,\"{$_POST['user']}\",\"{$_POST['title']}\",\"{$_POST['content']\",now())";  if (mysql_query($sql)) echo '成功!';
      ele echo mysql_error();
    }
    ?>
      

  2.   

    include("conn.php");if($_POST['submit']){
      $sql="insert into message(`id`,`user`,`title`,`content`,`lastdate`) values(NULL,\"{$_POST['user']}\",\"{$_POST['title']}\",\"{$_POST['content']}\",now())";  if (mysql_query($sql)) echo '成功!';
      ele echo mysql_error();
    }
      

  3.   


    我试了,还是不行,现在是可以写入留言,但是显示不出来留言!
    单独可以显示,而PHP和HTML混编就不显示了!
      

  4.   

    看错误是什么啊,看输出的HTML实际上是什么样子啊
      

  5.   

    //数据插入前要先转义非法字符,输出时要把html标签转义为字符实体:
    //数据插入前过滤非法字符:
    if($_POST['submit']){
      $sql='insert into message(`id`,`user`,`title`,`content`,`lastdate`) values(NULL,"'. mysql_real_escape_string ($_POST['user']).'","'. mysql_real_escape_string($_POST['title']).'","'. mysql_real_escape_string($_POST['content'])'.'",now())';
    //输出时处理html标签:
    <td>标题:<?php echo htmlspecialchars($row['title']); ?></td><td>用户名:<?php echo htmlspecialchars($row['user']); ?></td>
      

  6.   

    去买一本纸质的或下载一个电子版的关于PHP+MySQL编程的基础书籍从基础学起吧,这么几句代码,几乎没有一句写规范的。
      

  7.   


    显示留言代码:list.php
    <?php$conn= @ mysql_connect("localhost","root","") or die("数据库连接错误");
    mysql_select_db("bbs",$conn);
    mysql_query("set names GBK");?><table align="center" border="2" width="600" height="60" cellpadding="1" cellspacing="10" bgcolor="#99CCFF">
    <?
    $sql="select title from message";
    $query=mysql_query($sql);
    while($row=mysql_fetch_array($query)){?><tr bgcolor="#FFFFCC">
    <td>标题:<?=$row[title]?></td><td>用户名:<?=$row[user]?></td>
    </tr>
    <tr bgcolor="#66CCFF">
    <td>内容:<?=$row[content]?></td>
    </tr>
    <tr width="800" bgcolor="#3399CC"><td></td></tr>
    <?
    }
    ?>
    </table>
    修改的,试下显示留言代码:list.php
    <?php$conn= @ mysql_connect("localhost","root","") or die("数据库连接错误");
    mysql_select_db("bbs",$conn);
    mysql_query("set names GBK");?><table align="center" border="2" width="600" height="60" cellpadding="1" cellspacing="10" bgcolor="#99CCFF">
    <?
    $sql="select title from message";
    $query=mysql_query($sql);
    foreach($row=mysql_fetch_array($query) as $v){?><tr bgcolor="#FFFFCC">
    <td>标题:<?=$v[title]?></td><td>用户名:<?=$v[user]?></td>
    </tr>
    <tr bgcolor="#66CCFF">
    <td>内容:<?=$v[content]?></td>
    </tr>
    <tr width="800" bgcolor="#3399CC"><td></td></tr>
    <?
    }
    ?>
    </table>
      

  8.   

    呵呵···楼主哈···
    你在查询输出List.php页面里··
    sql语句是select title from message 那出来的就只会只有title这个字段的值哈
    你要想得到content和user这些  那你应该把这些全写上,或是select * from message
      

  9.   

    php代码块开始标签改为<?php
    也就是把你写的代码中的<?改为<?php