PHP - 插入数据库问题

时间:2012-12-17 05:41:41

标签: php database

我在编码的这一部分上已经停留了一段时间,因为更改要么出现语法错误,要么发出一条消息,说它是一个未知的列。我试过单引号而且没有,我想我之前尝试过双引号并且什么也没得到。我很确定问题出在查询部分,但我现在还不确定。

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)在已经在代码的前面部分添加引号之后。它现在运行良好〜

5 个答案:

答案 0 :(得分: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";
        } 

答案 1 :(得分:0)

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

答案 2 :(得分: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',因为它们是在您的代码中本地定义的。

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

答案 3 :(得分: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();

答案 4 :(得分: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'