2017-05-29 99 views
0

我正在为应用程序创建Web服务,但我坚持使用插入查询。其实我做了一个评分系统,并希望用wordpress标准在数据库中插入值。Wordpress插入查询不起作用

这里是我的查询:

$res = $wpdb->query($wpdb->prepare( 
       "INSERT INTO $table_name(rating_postid, rating_posttile, rating_rating, rating_username, rating_userid) VALUES (%d, %s, %d, %s, $d)", 
        array(
         $rating_postid, 
         $post_title, 
         $post_rating, 
         $user_name, 
         $rating_userid 
        ) 
       ) 
      ); 

,这里是另一个:

$res = $wpdb->insert(
       $table_name, 
       array(
        'rating_postid' => $rating_postid, 
        'rating_posttile' => $post_title, 
        'rating_rating' => $post_rating, 
        'rating_username' => $user_name, 
        'rating_userid' => $rating_userid 
       ) 
      ); 

但是,没有一个是工作,为什么?

if($res){ 
     echo 'inserted'; 
    }else{ 
     echo 'not inserted'; 
    } 

我得到其他部分alwasy

我用这些查询经常和他们合作我非常好,但我不知道这有什么错他们现在... :(

+1

太多的蜜蜂或蜡烛,或什么的!没有人认真,你已经给我们提供了我们可以合作的东西。你得到什么错误?你如何合并这些代码?你的代码的其余部分在哪里?你的代码的其余部分是什么? –

+0

更新了帖子。当我运行这个时没有错误。我只想知道查询是否正确? –

+0

@ManishNegi你的查询是正确的。 – purvik7373

回答

0

尝试这一点。

 $wpdb->insert(
      $table_name, 
      array(
       'rating_postid' => $rating_postid, 
       'rating_posttile' => $post_title, 
       'rating_rating' => $post_rating, 
       'rating_username' => $user_name, 
       'rating_userid' => $rating_userid 
      ), 
      array( 
       '%d', 
       '%s', 
       '%s', 
       '%s', 
       '%d' 
      ) 
     ); 
+0

也试过这个,但没有运气:( –

+0

检查错误'$ wpdb-> show_errors();' – vel

+0

如何使用这个$ wpdb-> show_errors();? –

0

声明$wpdb全球并用它来执行返回PHP对象的SQL查询语句

global $wpdb; 
$table_name = $wpdb->prefix . "YOUR_TABLE_NAME"; // Enter without prefix 
$data = array(
    'rating_postid' => $rating_postid, 
    'rating_posttile' => $post_title, 
    'rating_rating' => $post_rating, 
    'rating_username' => $user_name, 
    'rating_userid' => $rating_userid 
); 

$result = $wpdb->insert($table_name, $data); 
if($result){ 
    echo "Inserted..!"; 
}else{ 
    echo "Something wrong..!"; 
    $wpdb->show_errors(); 
} 
+0

也运行这个,并得到“有问题..” –

+0

@ManishNegi你设置这个字段'rating_postid'作为主键? – purvik7373

+0

@ManishNegi添加此行'$ wpdb-> show_errors();'在'else'语句中 – purvik7373