2013-08-25 72 views
0

当我尝试在我的论坛这个要发布的主题上来:列数不匹配,第1行(错误)值计数

列数并不在行1

匹配值计数

有人可以看到错误? TNX :)

这是我的代码:

<?php 
session_start(); 
if ($_SESSION['uid'] == "") { 
    header("Location: index.php"); 
    exit(); 
} 
if (isset($_POST['topic_submit'])) { 
    if (($_POST['topic_title'] == "") && ($_POST['topic_content'] == "")) { 
     echo "You did not fill in both fields. Please retun to the previous page."; 
     exit(); 
    } else {  
     include_once("connect.php"); 
     $cid = $_POST['cid']; 
     $title = $_POST['topic_title']; 
     $content = $_POST['topic_content']; 
     $creator = $_SESSION['uid']; 
     $sql = "INSERT INTO topics (category_id, topic_title, topic_creator, topic_date, topic_reply_date) VALUES ('".$cid."', '".$title."', '".$creator."', now(), now())"; 
     $res = mysql_query($sql) or die(mysql_error()); 
     $new_topic_id = mysql_insert_id(); 
     $sql2 = "INSERT INTO posts (category_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$new_topic_id."', '".$creator."', '".$content."', now())"; 
     $res2 = mysql_query($sql2) or die(mysql_error()); 
     $sql3 = "UPDATE categories SET last_post_date=now(), last_user_posted='".$creator."' WHERE id='".$cid."', LIMIT 1"; 
     $res3 = mysql_query($sql3) or die(mysql_error()); 
     if (($res) && ($res2) && ($res3)) { 
      header("Location: view_topic.php?cid=".$cid."&ti­d=".$new_topic_id); 
     } else { 
      echo "There was a problem creating your topic. Please try again."; 
     } 
    } 
} 
?> 
+1

你的第二个插入体具有字段列表4场,但你的'VALUES'它 –

+0

有5个值tnx @PatrickEvans –

回答

0

就出在这里你的错误:

$sql2 = "INSERT INTO posts (category_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$new_topic_id."', '".$creator."', '".$content."', now())"; 

你插入5个值($cid$new_topic_id$creator$contentnow())到4列(category_id,post_creator,post_content,post_date)。因此,列数 (4)不匹配值计数(5)。

此外,您可能会遇到在这里另一个错误,因为你有LIMIT前一个额外的逗号:

$sql3 = "UPDATE categories SET last_post_date=now(), last_user_posted='".$creator."' WHERE id='".$cid."', LIMIT 1"; 
+0

tnx为逗号错误! :D –

+1

@SebastianThune [将答案标记为已接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work),如果它是正确的...... :) – Nalaka526

相关问题