1

一段时间以来,我一直停留在我的编码的这一部分上,因为更改要么出现语法错误,要么出现一条表明它是未知列的消息。我尝试过使用单引号和不使用单引号,我想我之前尝试过双引号但一无所获。我很确定问题出在查询部分,但目前我不确定。

include_once "library.php";
connectdatabase();

if (isset($_COOKIE['bunny_id'])) {
    $poster = $_COOKIE['bunny_id'];
} else {
    echo "Sorry, you need to login or sign up before you can post :C";
}

if (!empty($_POST['topic_name'])) {
    $topic_name = "'" . addslashes($_POST['topic_name']) . "'";
} else {
    $topic_name = 'NULL';
    echo "Topic field is not filled :c \n";
    exit;
}

if (!empty($_POST['post_content'])) {
    $post_content = "'" . addslashes($_POST['post_content']) . "'";
} else {
    $post_content = 'NULL';
    echo "Post content is not filled :c \n";
    exit;
}

$query  = "INSERT INTO posts(messages_poster, messages_topic, messages_content) VALUES('$poster', '$topic_name', '$post_content')";
$query2 = mysql_query($query);

if (mysql_num_rows($query2) == 1) {
    echo "Topic has been created~";
    exit;
} else {
    echo "Sorry, there's been an error :c";
}

编辑:我设法解决了这个问题。该问题与在代码的前面部分中添加引号后向“$topic_name”和“$post_content”添加单引号有关(较小的问题还涉及使用 mysql_num_rows 而不是 mysql_affected_rows)。现在可以正常运行了~

4

5 回答 5

1

尝试这个

 $query = "INSERT INTO posts(messages_poster, messages_topic, messages_content) VALUES('$poster', '$topic_name', '$post_content')";
    $query2=mysql_query($query);

         if(mysql_num_rows($query2) == 1) {

        echo "Topic has been created~";
    exit; 
    } else { 
        echo "Sorry, there's been an error :c";
        } 
于 2012-12-17T05:44:15.390 回答
0

我手头没有 DBMS 来测试它,但第一次看代码让我觉得错误可能在于尝试构建/构造查询:

$query = "INSERT INTO posts(messages_poster, messages_topic, messages_content) VALUES('$poster', '$topic_name', '$post_content')";

请注意,您需要将查询传递给另一个子程序(将在其中处理,即提交给 DBMS),如下所示:

$query = "INSERT INTO posts(messages_poster, messages_topic, messages_content) VALUES( 'val1',  'val2', 'val3')"; so the trick is in trying to translate $poster -> 'val1';
$topic_name -> 'val2'
$post_content-> 'val3'
于 2012-12-17T17:49:41.817 回答
0

您应该确保您的三列(messages_poster, messages_topic, messages_content)包含在您的表中posts。还要检查查询mysql_query($query) or die(mysql_error())以确保错误是什么。

于 2012-12-17T05:50:45.580 回答
0

尝试这个 :-

   include_once "library.php";`
  connectdatabase();`
  $poster = '';
 if(isset($_COOKIE['bunny_id'])){
        $poster= $_COOKIE['bunny_id'];
        } 
         else {
            echo "Sorry, you need to login or sign up before you can post :C";
        } 
  $topic_name = '';
  if (!empty($_POST['topic_name'])){
     $topic_name = "'". addslashes($_POST['topic_name']) . "'";
} else {
    $topic_name = 'NULL';   
    echo "Topic field is not filled :c \n";
    exit;
}   
$post_content = '';
    if (!empty($_POST['post_content'] )) { 
    $post_content = "'". addslashes ( $_POST['post_content'] ) . "'";
} else {
    $post_content = 'NULL'; 
    echo "Post content is not filled :c \n" ;
    exit;
} 

        $query = "INSERT INTO posts(messages_poster, messages_topic, messages_content) VALUES('$poster', '$topic_name', '$post_content')";
        $query2=mysql_query($query);

             if(mysql_num_rows($query2) == 1) {

            echo "Topic has been created~";
        exit; 
         } else { 
            echo "Sorry, there's been an error :c";
            } 

问题可能是您没有获得这些值 '$poster'、'$topic_name'、'$post_content',因为它们是在代码中本地定义的。

向我们展示您的错误。未知列表示您的表列名称不正确。

于 2012-12-17T05:51:09.013 回答
0
 include_once "library.php";`
  connectdatabase();`

 if(isset($_COOKIE['bunny_id'])){
        $poster= $_COOKIE['bunny_id'];
        } 
         else {
            echo "Sorry, you need to login or sign up before you can post :C";
        } 

  if (!empty($_POST['topic_name'])){
     $topic_name =  addslashes($_POST['topic_name']); // we no need to concat string
} else {
    $topic_name = '';   
    echo "Topic field is not filled :c \n";
    exit;
}   

    if (!empty($_POST['post_content'] )) { 
    $post_content = addslashes ( $_POST['post_content'] ) ;
} else {
    $post_content = ''; 
    echo "Post content is not filled :c \n" ;
    exit;
} 

        `$query = "INSERT INTO posts (messages_poster, messages_topic, messages_content) VALUES('$poster', '$topic_name', '$post_content')";
        $query2=mysql_query($query) or die("could not inserted");`
         $lastid = mysql_insert_id();
于 2012-12-17T05:51:13.227 回答