本人菜鸟做的一个简单的留言本。数据插入功能可以实现。但是显示不出来库里的数据。以下是代码:
/******** conn.php ********/
       $conn= @ mysql_connect("localhost","root","") or die("数据库连接错误".mysql_error());
mysql_select_db("message",$conn);
mysql_query("set names gb2312");

/******** liuyan.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="file.php" method="post">
      用  户:
      <input type="text" name="user" value="" size="10" maxlength="40"/><br>
      标  题:
      <input type="text" name="title" value=""size="20" maxlength="40"/><br>
      内  容:
      <textarea name="content" rows="10" cols="30" ></textarea><br>
   <input type="submit" name="submit" value="发表留言"/>
  </form>
/********* show.php ***********/
<?php
include("conn.php");
?> <table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
<?
$sql="select * from message";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){?>
   <tr bgcolor="#eff3ff">
   <td>标题:<?=$row[title]?> 用户:<?=$row[user]?></td>
   </tr>
   <tr bgColor="#ffffff">
   <td>内容:<?=$row[content]?></td>
   </tr>
<?
    }
?>
</table>

解决方案 »

  1.   

    代码我大致看了下,暂时没发现问题。
    你的PHP有没有设置 短标签 如果没有的话 用<?... ?>是不可以的,要用<?php....?>才会显示出来不知道加上后可不可以,你可以试一下<?php
    include("conn.php");
    ?><table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef"><?php$sql="select * from message";
    $query=mysql_query($sql);
    while($row=mysql_fetch_array($query)){?>
    <tr bgcolor="#eff3ff">
    <td>标题:<?php=$row[title]?> 用户:<?php=$row[user]?></td>
    </tr>
    <tr bgColor="#ffffff">
    <td>内容:<?php=$row[content]?></td>
    </tr><?php
    }
    ?>
    </table> 
     
     
      

  2.   

    把“=”改成echo 看行不行啊
    <tr bgcolor="#eff3ff">
    <td>标题:<?php=$row[title]?> 用户:<?php=$row[user]?></td>
    </tr>
    <tr bgColor="#ffffff">
    <td>内容:<?php=$row[content]?></td>
    </tr>
      

  3.   

    ******** conn.php ********/
    @mysql_connect("localhost","root","") or die("数据库连接错误".mysql_error());
    @mysql_select_db("message") or die("数据库选择错误".mysql_error());
    mysql_query("set names 'gb2312'");
    /******** liuyan.php **********/
    <?php
    include("conn.php");if($_POST['submit']){
    $sql="insert into `message` (`user`,`title`,`content`,`lastdate`)" .
    " values('$_POST[user]','$_POST[title]','$_POST[content]',now())";
    mysql_query($sql);
    echo"发表成功";
    }
    ?>
    <form action="liuyan.php" method="post">
    用 户:
    <input type="text" name="user" value="" size="10" maxlength="40"/><br>
    标 题:
    <input type="text" name="title" value=""size="20" maxlength="40"/><br>
    内 容:
    <textarea name="content" rows="10" cols="30" ></textarea><br>
    <input type="submit" name="submit" value="发表留言"/>
    </form>/********* show.php ***********/
    <?php
    include("conn.php");
    ?><table width=500 border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#add3ef">
    <?php
    $sql="select * from message";
    $query=mysql_query($sql);
    while($row=mysql_fetch_array($query)){?>
    <tr bgcolor="#eff3ff">
    <td>标题:<?php echo $row['title'];?> 用户:<?php echo $row['user'];?></td>
    </tr>
    <tr bgColor="#ffffff">
    <td>内容:<?php echo $row['content'];?></td>
    </tr>
    <?php
    }
    ?>
    </table>
      

  4.   

    建议LZ不要用gb2312,用utf-8,utf-8这个比较通用。
      

  5.   

    差不多就是这两点,一般的编码格式建议楼主采用utf-8格式的,还有把你那个=号改成echo看可以不,我不知道你那个=号是干什么用的
      

  6.   

    我改了。改成了---<?php echo $row['title'];?>。。这种格式也不行啊。怎么回事呢??改成UTF-8怎么改呢??改数据库吗??还是页面编码啊??数据库那个GBK  和GB2312什么区别啊??
      

  7.   

    显示不出来?
    报错还是仅仅
    $sql="select * from message";
    $query=mysql_query($sql);
    while($row=mysql_fetch_array($query)){
    ******//这里的内容没显示出来?
    }
      

  8.   

    不报错。。就是什么也不显示。只显示HTML的表格内容。数据库中的数据什么都没读取出来。我感觉代码没什么错误啊。。这让人头疼。。不知道什么原因啊。
      

  9.   

    我运行了一下你的代码,可以执行啊,可以显示我的数据库里的内容,你看下是不是你的conn.php那个文件连接数据库写错了,以至于没有显示出结果
    最好把你的conn那个文件贴出来,让大家找下错误
      

  10.   

    不好意思,刚看到你的conn已经贴出来了
    mysql_query("SET NAMES UTF-8");
    然后看你的数据库是什么格式的还有你的页面开始的时候加上<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            </head>标准点