这是PHP+MySQL八大动态Web应用实战中的代码可插不进数据,数据库中没插入数据的,请高手看一下哪错了?谢谢。
有时也会抛错误Cannot modify header information - headers already sent by 在这行
header("Location:".$config_basedir."/viewmessages.php?id=".$topicid);在这句中$topicsql = "INSERT INTO topics(date, user_id, forum_id, subject) VALUES(NOW(), ".$_SESSION['USERID'].", ".$validforum.", '".$_POST['subject']."');";的$validforum好像变成空值了。
<?php
/*
 * Created on 2010-12-4
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 session_start(); require("config.php");
 require("functions.php"); $db = mysql_connect($dbhost,$dbuser,$dbpassword);
 mysql_select_db($dbdatabase,$db); $forchecksql = "SELECT * FROM forums;";
 $forcheckresult = mysql_query($forchecksql);
 $forchecknumrows = mysql_num_rows($forcheckresult); if($forchecknumrows == 0) {
  header("Location:".$config_basedir);
 } if(isset($_GET['id']) == true) {
  if(is_numeric($_GET['id']) == FALSE) {
  $error = 1;
  }
  if($error == 1) {
  header("Location:".$config_basedir);
  }
  else {
  $validforum = $_GET['id'];
  }
 }
 else {
  $validforum = 0;
 } if(isset($_SESSION['USERNAME']) == FALSE) {
  header("Lcation:".$config_basedir."/login.php?ref=newpost&id=".$validforum);
 } if($_POST['submit']) {  if($validforum == 0) {
  $topicsql = "INSERT INTO topics(date, user_id, forum_id, subject) VALUES(NOW(), ".$_SESSION['USERID'].", ".$_POST['forum'].", '".$_POST['subject']."');";
  }
  else {
  $topicsql = "INSERT INTO topics(date, user_id, forum_id, subject) VALUES(NOW(), ".$_SESSION['USERID'].", ".$validforum.", '".$_POST['subject']."');";
  }    mysql_query($topicsql);
    $topicid = mysql_insert_id();    $messagesql = "INSERT INTO messages(data, user_id, topic_id, subject, body) VALUES(NOW(), ".$_SESSION['USERID'].", ".mysql_insert_id().", ".$_POST['subject'].", '".$_POST['body']."');";
    echo "$messagesql"
    header("Location:".$config_basedir."/viewmessages.php?id=".$topicid);
 }
 else {
  require("header.php");  if($validforum != 0) {
  $namesql = "SELECT name FROM forums ORDER BY name;";
  $nameresult = mysql_query($namesql);
  $namerow = mysql_fetch_assoc($nameresult);
  echo "<h2>Post new message to the :".$namerow['name']." forum</h2>";
  }
  else {
  echo "<h2>Post a new message</h2>";
  }?><form action="<?php echo  pf_script_with_get($SCRIPT_NAME); ?>" method="post">
<table>
<?phpif($validforum == 0) {
$forumssql = "SELECT * FROM forums ORDER BY name;";
$forumsresult = mysql_query($forumssql);
?>
<tr>
<td>Forums</td>
<td>
<select name="forum">
<?php
while($forumsrow = mysql_fetch_assoc($forumsresult)){
echo "<option value='".$forumsrow['id']."'>".$forumsrow['name']."</option>";
}
?>
</select>
</td>
</tr>
<?php
}
?> <tr>
<td>Subject</td>
<td><input type="text" name="subject"></td>
</tr>
<tr>
<td>Body</td>
<td><textarea name="body" rows="10" cols="50"></textarea></td>
</tr>
<td></td>
<td><input type="submit" name="submit" value="Post!"></td>
</table>
</form><?php
 }
 require("footer.php");
?>

解决方案 »

  1.   


        echo "$messagesql"
        header("Location:".$config_basedir."/viewmessages.php?id=".$topicid);
    header();调用之前不能有任何输出!否则报错!这里很明显在之前echo输出了!
    $validforum的值是根据传递过来的id获取的,如果没有传id参数则默认为零然后在判断里拼凑了一条sql,
    如果传了id,而且是数字类型的,则执行你说的那条sql.
    至于具体是什么值你exit($validforum);就知道了!
      

  2.   

    夜深了!果断sleep!see you tomorrow!
      

  3.   


    header("Location:".$config_basedir);像这种跳转语句改成使用 javascript来跳转
      

  4.   


         if($validforum == 0) {
             $topicsql = "INSERT INTO topics(date, user_id, forum_id, subject) VALUES(NOW(), ".$_SESSION['USERID'].", ".$_POST['forum'].", '".$_POST['subject']."');";//这里的$_POST['subject']以字符串插入
         }
         else {
             $topicsql = "INSERT INTO topics(date, user_id, forum_id, subject) VALUES(NOW(), ".$_SESSION['USERID'].", ".$validforum.", '".$_POST['subject']."');";
         }    mysql_query($topicsql);
        $topicid = mysql_insert_id();    $messagesql = "INSERT INTO messages(data, user_id, topic_id, subject, body) VALUES(NOW(), ".$_SESSION['USERID'].", ".mysql_insert_id().", ".$_POST['subject'].", '".$_POST['body']."');";//这里的$_POST['subject']以数字插入