留言板不能向MYSQL插入数据,可直接从mysql插入数据后却能在留言板显示。php与mysql都用的是gb2312.怎么回事啊?真是要命,刚解决了乱码,又有新的问题。可能女孩子真的不适合做编程,考虑是否该转行了。~超级郁闷中~
php代码如下: // 连接mysqlserver
   $DBConn = mysql_connect($DBhost,$DBuser,$DBpass) or die("Error in GuestBook Application: " . mysql_error());
     mysql_query("SET NAMES  'GBK' ");
   // Select mySQL Database
   mysql_select_db($DBName, $DBConn) or die("Error in GuestBook Application: " . mysql_error());// Part Two - 分两段写和读
   $action = $_GET['action'];
   
   switch($action) {
      case 'read' :
 //从数据库中获取数据
 $sql = 'SELECT * FROM `' . $table . '`';
 $allComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
 $numallComments = mysql_num_rows($allComments);

 $sql .= ' ORDER BY `time` DESC LIMIT ' . $_GET['NumLow'] . ', ' . $numComments;
 $fewComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
 $numfewComments = mysql_num_rows($fewComments);
 //输出flash可读的信息
 print '&totalEntries=' . $numallComments . '&';
 print "<br>&entries=";
 
 if($numallComments == 0) {
    print "到现在为止还没有留言..";
 } else { 
    while ($array = mysql_fetch_array($fewComments)) {
   $name = mysql_result($fewComments, $i, 'name');
   $email = mysql_result($fewComments, $i, 'email');
   $comments = mysql_result($fewComments, $i, 'comments');
   $time = mysql_result($fewComments, $i, 'time');
   
   print '网名: ' . $name . '       日期: ' . $time . '<br>邮箱: ' . $email . '<br><b>留言内容: </b><font color="#ffffff">' . $comments . '</font><br><br>';
   $i++;
    }
}
// 当没有信息时显示下面的信息
if($_GET['NumLow'] > $numallComments) {
   print '没有更多的条目!&';
}
break;
 
  case 'write' :
     // flash端接收的数据
 $name = ereg_replace("&", "%26", $_POST['yourname']);
 $email = ereg_replace("&", "%26", $_POST['youremail']);
 $comments = ereg_replace("&", "%26", $_POST['yourcomments']);
 $submit = $_POST['submit'];
   
 // 当前系统日期
 $submitted_on = date ("Y-m-d H:i:s",time());
   
 // 检查是否从flash端提交
 if($submit == 'Yes'){
 // 插入数据
 $sql = 'INSERT INTO ' . $table . 
                ' (`ID`, 
   `name`, 
   `email`, 
   `comments`, 
   `time`
  ) 
  VALUES 
  (\'\','
   . '\'' . $name . '\',' 
   . '\'' . $email . '\',' 
   . '\'' . $comments . '\',' 
   . '\'' . $submitted_on . '\'
   )';
 $insert = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
 
 print "&gb_status=谢谢您的留言!.&done=yes&";
 return;
 }
 print "&_root.write.gb_status=Error!&";
 break;
   }
?>

解决方案 »

  1.   

    $insert = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error()); 
    把mysql_error()放到insert 后面
    然后echo出来,看错误信息
      

  2.   

    try : $sql = "INSERT INTO  $table
                  (`name`, 
                   `email`, 
                   `comments`, 
                   `time` 
                  ) 
                VALUES 
               (  '$name', 
                  '$email', 
                  '$comments', 
                  '$submitted_on' 
               )"; 
      

  3.   

     VALUES 
      (\'\',' 
      . '\'' . $name . '\',' 
      . '\'' . $email . '\',' 
      . '\'' . $comments . '\',' 
      . '\'' . $submitted_on . '\' 
      )'; 这部分不要用单引号改成 $sql = "INSERT INTO `$table` set `ID`='', `name`='$name', `email`='$email', `comments`='$comments', `time`='$submitte_on'";