2013-08-25 103 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

相關問題