在做一个入门级的PHP5+MySQL5新闻发布系统。到了实现新闻评论这个功能的阶段了。下面是一些必要的说明:
新闻表:字段为news_id,news_title,news_content,news_time,news_operator,news_comment,comment_id.
思路是这样:写入的新闻评论(用fckeditor编辑器)和新闻id对应起来,查看某个新闻评论时用SQL语句把对应id的评论单独用一个页面列出来。
相关代码:
../client/news_show.php(文件名)
<?php
require("../global.php");  //MySQL访问类,公共函数和创建数据库访问对象:$DB=new DB_MySQL
if($_GET||$_POST)
{
    //查询新闻内容
    //$id = $_GET['news_id'];
    $id = $_GET['$news_id'];
    $sql = "select * from news where news_id = ".$id;
    $news = $DB->fetch_one_array($sql);
    //查询图片
    $sql = "select * from news_pic where nepi_news_id = ".$id;
    $pic = $DB->fetch_one_array($sql);    
    
    $comment_id=$id;
    $news_comment = $_POST['FCKeditor1'];
    $sql = "insert news (comment_id,news_comment)";
    $sql .=" values('".$comment_id."','".$news_comment."')";
    $DB->query($sql);//开始往数据库中插入新闻评论
    $url = '../client/news_show.php';
    redirect_once($url);    
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 新闻浏览 </TITLE>
<META NAME="Chinacalf" CONTENT="[email protected]">
<meta http-equiv="Content-Type" content="text/html;    charset=gb2312">
</HEAD>
<BODY>
<table width="888px" align="center" border="1" cellspacing="0" cellpadding="0" >
  <tr>
<!---- ******左侧部分开始****** -----> 
    <? require("../html/client_left.php") ?>
<!---- ******左侧部分结束****** ---->
  <td width="2px" > </td>
<!---- ******右侧部分开始****** ---->
<td align="center" ><table width="100%" cellpadding="0" cellspacing="0" bgcolor="#eeeeee">
    <tr height='32px'  bgcolor='#ccddee'> 
    <td width="100%" align="center">
    <!-- 显示新闻标题 -->
    <?php echo $news['news_title'] ?>    
    </td>
    </tr>
    <!-- 显示新闻发布时间 -->
    <tr height='32px'>
    <td align="center"><?php echo $news['news_time'] ?>    
    </td>
    </tr>
  
  <tr height='34px'  align="center"> <!-- 新闻发布时间和新闻内容之间的间隔控制 -->
  
    
    <!-- 显示新闻内容 -->
    <td align="left"><?php echo $news['news_content'] ?></td>
    <td width ='10'></td>
  </tr>
  <tr>
    <td colspan=3 align="center">
    <!-- 显示新闻图片 -->
    <img src="<?php echo $pic['nepi_file_name'] ?>">
    </td>
    </tr>
  
    <tr height='10'><td colspan=3> </td></tr>
    <tr align="center">
    <td><a href="../client/index.php">返回首页</a></td>
    </tr><tr><td>我要评论</td></tr>
<form name="news_comment"  action="./news_show.php" onSubmit="return (check_form())" method="post">
<tr><td  width="100%">
<?php
include("../fckeditor/fckeditor.php") ;
$oFCKeditor = new FCKeditor('FCKeditor1') ;  // 创建FCKeditor实例,可创建多个实例
$oFCKeditor->BasePath = '../fckeditor/';      // 设置FCKeditor目录地址
$oFCKeditor->Width = '100%' ; 
$oFCKeditor->Height = '200' ; //设置显示高度
$oFCKeditor->Create() ;                      // 创建编辑器       
?>
</td>
</tr> 
<tr>
<td><input type="submit" value=" 发表留言 " size="8" onclick="return (check_form())">
</td>
</tr>
</form>  
</table></td>
</tr>
</table></BODY>
</HTML>
页面显示当然是正常的,一旦在编辑器中输入留言,按下“发表留言后”,就会出现这样的错误提示:
数据库发生错误: SQL查询语句出错: select * from news where news_id = 
MySQL 返回错误信息: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 
脚本终止         
疑惑的是,如果“select * from news where news_id =”这句有问题,那么新闻内容根本就已经显示不出来了。小弟驽钝,还望朋友牛人们不吝赐教,谢谢!

解决方案 »

  1.   

    1 $id = $_GET['$news_id']; 应该是 $id = $_GET['news_id'];
    2 你访问新闻的链接地址有误.
      

  2.   

    我错了,所以我作了点修改, 
    新闻表:字段为news_id,news_title,news_content,news_time,news_operator,news_comment,comment_id. 
    思路是这样:写入的新闻评论(用fckeditor编辑器)和新闻id对应起来,查看某个新闻评论时用SQL语句把对应id的评论单独用一个页面列出来。 
    相关代码: 
    ../client/news_show.php(文件名) 
    <?php 
    require("../global.php");  //MySQL访问类,公共函数和创建数据库访问对象:$DB=new DB_MySQL 
    if($_GET)
    {
        //查询新闻内容
     $id = $_GET['news_id'];
     $sql = "select * from news where news_id = ".$id;
     $news = $DB->fetch_one_array($sql);
     //查询图片
     $sql = "select * from news_pic where nepi_news_id = ".$id;
     $pic = $DB->fetch_one_array($sql);
     

    if($_POST)
    {
     $comment_id=$_GET['news_id']; 
     $news_comment = $_POST['FCKeditor1'];
     $sql = "insert news ($comment_id,news_comment)";
     $sql .=" values('".$comment_id."','".$news_comment."')";
     $DB->query($sql);//开始往数据库中插入新闻评论
        $url = '../client/index.php';
     redirect_once($url);
    }
    ?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
    <HTML> 
    <HEAD> 
    <TITLE> 新闻浏览 </TITLE> 
    <META NAME="Chinacalf" CONTENT="[email protected]"> 
    <meta http-equiv="Content-Type" content="text/html;    charset=gb2312"> 
    </HEAD> 
    <BODY> 
    <table width="888px" align="center" border="1" cellspacing="0" cellpadding="0" > 
      <tr> 
    <!---- ******左侧部分开始****** -----> 
        <? require("../html/client_left.php") ?> 
    <!---- ******左侧部分结束****** ----> 
      <td width="2px" > </td> 
    <!---- ******右侧部分开始****** ----> 
    <td align="center" > <table width="100%" cellpadding="0" cellspacing="0" bgcolor="#eeeeee"> 
        <tr height='32px'  bgcolor='#ccddee'> 
        <td width="100%" align="center"> 
        <!-- 显示新闻标题 --> 
        <?php echo $news['news_title'] ?>     
        </td> 
        </tr> 
        <!-- 显示新闻发布时间 --> 
        <tr height='32px'> 
        <td align="center"><?php echo $news['news_time'] ?>     
        </td> 
        </tr> 
       
      <tr height='34px'  align="center"> <!-- 新闻发布时间和新闻内容之间的间隔控制 --> 
       
         
        <!-- 显示新闻内容 --> 
        <td align="left"><?php echo $news['news_content'] ?></td> 
        <td width ='10'></td> 
      </tr> 
      <tr> 
        <td colspan=3 align="center"> 
        <!-- 显示新闻图片 --> 
        <img src="<?php echo $pic['nepi_file_name'] ?>"> 
        </td> 
        </tr> 
       
        <tr height='10'><td colspan=3> </td></tr> 
        <tr align="center"> 
        <td><a href="../client/news_show.php">返回首页</a></td> 
        </tr> <tr><td>我要评论</td></tr> 
    <form name="news_comment"  action="./news_show.php" onSubmit="return (check_form())" method="post"> 
    <tr><td  width="100%"> 
    <?php 
    include("../fckeditor/fckeditor.php") ; 
    $oFCKeditor = new FCKeditor('FCKeditor1') ;  // 创建FCKeditor实例,可创建多个实例 
    $oFCKeditor->BasePath = '../fckeditor/';      // 设置FCKeditor目录地址 
    $oFCKeditor->Width = '100%' ; 
    $oFCKeditor->Height = '200' ; //设置显示高度 
    $oFCKeditor->Create() ;                      // 创建编辑器       
    ?> 
    </td> 
    </tr> 
    <tr> 
    <td><input type="submit" value=" 发表留言 " size="8" onclick="return (check_form())"> 
    </td> 
    </tr> 
    </form>   
    </table> </td> 
    </tr> 
    </table> </BODY> 
    </HTML> 页面显示是正常的,一旦在编辑器中输入留言,按下“发表留言”,就会出现这样的错误提示: 
    数据库发生错误: SQL查询语句出错: insert news (,news_comment) values('',' 评论内容') 
    MySQL 返回错误信息: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'news_comment) values('',' 
    评论内容 
    ')' at line 1 
    脚本终止 我要怎么做才可以成功啊...很想知道到底哪里出了问题!!
      

  3.   

    $sql = "insert news ($comment_id,news_comment)";
    //1、insert into 表名
    //2、$comment_id 这里应该是字段名 可能想表述的是comment_id$sql .=" values('".$comment_id."','".$news_comment."')"; 
    //3、$comment_id应该是一个数值,这里为空。请检查前面的程序
      

  4.   


    1.插入时确实是错误的,漏了你所说的into
    2.我又错了,本意是想在字段comment_id,news_comment插入的是评论对应的新闻序号和评论内容
    3.前面两个错误该过来后,不明白$comment_id的数值为什么为空。是因为$comment_id=$_GET['news_id']; 传值时就出了语法错误?那么要如何修改才能得到当前新闻的id以赋值给$comment_id呢?
      

  5.   

    修改好1,2两个错误后,再一次输入评论内容时,此时的错误提示是:
    数据库发生错误: SQL查询语句出错: insert into news (comment_id,news_comment) values('','
    评论内容')
    MySQL 返回错误信息: Field 'news_id' doesn't have a default value 
    脚本终止
      

  6.   

    if($_POST)//here $_POST
    {
    $comment_id=$_GET['news_id']; 
    $news_comment = $_POST['FCKeditor1'];
    $sql = "insert news ($comment_id,news_comment)";
    $sql .=" values('".$comment_id."','".$news_comment."')";
    $DB->query($sql);//开始往数据库中插入新闻评论
        $url = '../client/index.php';
    redirect_once($url);
    } $comment_id的值为空,这里的$_GET['news_id']可能要改成$_POST['news_id']; 
      

  7.   

    这个先前我已经尝试过,在if(_POST)中即使$_GET['news_id']改成$_POST['news_id'];也不行
    我也试过引入全局变量,如下
    if($_GET)
    {
        //查询新闻内容
    $id = $_GET['news_id'];
    //global $global_id=$id;这是尝试过的,不行
    //global $global_id=$_GET['news_id'];
    $sql = "select * from news where news_id = ".$id;
    $news = $DB->fetch_one_array($sql);
    //查询图片
    $sql = "select * from news_pic where nepi_news_id = ".$id;
    $pic = $DB->fetch_one_array($sql);

    }
    global $global_id=$_GET['news_id'];//放在后面仍然不行,因为至少这样连具体的新闻内容都看不到,别说要评论了~
    if($_POST)
    {
    ...

    有点灰心了~嗯,坚信大家会有办法的!
      

  8.   

    你看一下表单是用POST提交的,所以if($_GET){...}里面的语句此时不被执行。真正的问题在于你在表单里并没有提交news_id的内容,那后面的$_POST['news_id']肯定是取不到所需要的值的
      

  9.   

    另外向大家道个歉,我修改时大意仍没有加上into.我这样很不严谨很不认真!对不起...