WordPress wpdb->插入/更新无效

时间:2013-09-04 11:45:06

标签: php mysql wordpress

我目前正在使用WordPress插件,并且已经使用以下样式创建了一个带有插件的表:

   $sql = "CREATE TABLE IF NOT EXISTS $table_name (
   id INTEGER NOT NULL AUTO_INCREMENT,
   post_id INTEGER NOT NULL,
   lat FLOAT NOT NULL,
   lng FLOAT NOT NULL,
   str VARCHAR(55), 
   plz INTEGER, 
   ort VARCHAR(20), 
   UNIQUE KEY id (id) )

我可以看到db通过“MySQL Query Browser”存在......

并希望使用以下代码在表中插入一些信息:

(str,plz,ort还没有必要,所以我想让它们变空了......)

而且我相信每次插入内容时id都会自动增加,即使这样我也不确定我是否需要id作为键,因为每个帖子只有一个条目,所以post_id可以是关键......

function install_data($latlng, $post_id) {
   global $wpdb;

   if ( ! $wpdb->update($table_name, array('lat' => $latlng[0], 'lng' => $latlng[1]), array('post_id' => $post_id))) 
   {       
   $wpdb->insert( $table_name, array('post_id' => $post_id, 'lat' => $latlng[0], 'lng' => $latlng[1]));
   } 
$wpdb->show_error();
$wpdb->print_error();
}

我尝试了两个错误..但是还没有在控制台中找到一个......也没有在error_log中看到...

我还尝试再次删除更新功能,但它仍然没有帮助...

好的......

这有点奇怪,但我解决了一半xD

function install_data($latlng, $post_id) {
   global $wpdb;

   require_once( ABSPATH . 'wp-config.php');

   if ( ! $wpdb->update($table_name, array('lat' => $latlng[0], 'lng' => $latlng[1]), array('post_id' => $post_id))) 
   {       
   $wpdb->insert( $table_name, array('post_id' => $post_id, 'lat' => $latlng[0], 'lng' => $latlng[1]));
   } 
$wpdb->show_error();
$wpdb->print_error();
}

现在只有更新无效...

它再次停止工作..我不知道为什么...... 此外,阅读也无效...

function read_data($post_id) {
    global $wpdb;

    require_once( ABSPATH . 'wp-config.php'); //Just a try, cause insert worked afterwards... for a short time -.-

    $latlng = $wpdb->get_results( "SELECT lat, lng FROM $table_name WHERE post_id = '$post_id'" );

    return $latlng;
}

1 个答案:

答案 0 :(得分:0)

       function socialMediaFacebook(){
                require_once(ABSPATH .'/wp-load.php');
                require_once(ABSPATH .'/wp-config.php');
                global $wpdb;
                $idvideonova = 0;
                 $app_id = $_POST['app_id'];
                 $app_link = $_POST['app_link'];
                 $app_caption = $_POST['app_caption'];
                 $app_description = $_POST['app_description'];
                    $table_name = $wpdb->prefix . "tableBloo;
                    $rows_affected = $wpdb->insert( $table_name, array('id' => $idvideonova, 'app_id' => $app_id, 'app_link' => $app_link, 'app_caption' => $app_caption, 'app_description' => $app_description) );
                    $lastid = $wpdb->insert_id;    
                         echo "  <script type=\"text/javascript\">
                                  alert('Social Media Settings Saved');
                                  </script>
                              ";

我也经历过......嘿嘿试着回想起你在哪里。或使用我的代码..帖子是文本框的名称.. 标签

相关问题