简体   繁体   English

WordPress wpdb->插入/更新不起作用

[英]WordPress wpdb->insert/update isn't working

I'm working on a WordPress Plugin currently, and already created a table with my plugin in the following style: 我目前正在使用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) )

I can see that the db exists through the "MySQL Query Browser"... 我可以通过“ MySQL查询浏览器”看到数据库存在。

And want to insert some infos into the table with the following code: 并希望使用以下代码将一些信息插入表中:

(str, plz, ort aren't necessary yet, so I wanted to let them be empty yet...) (str,plz和ort不是必需的,所以我想让它们为空...)

And I believe that the id will increase automatically every time I insert something, even so that I'm not quite sure if I even need the id as key, because every post will only have one entry, so that the post_id could be the key too... 而且我相信每次插入内容时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();
}

I tried both errors.. But haven't seen one in the console... nor in the error_log... 我尝试了两种错误..但是没有在控制台中看到一个...也没有在error_log中看到...

I also tried to remove the update feature again, but it still hasn't helped anything... 我也尝试再次删除更新功能,但仍然没有任何帮助...

Okay.. 好的..

That's kind of weird but I solved half of it 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();
}

Now it's only the update that is not working... 现在只有更新无法正常工作...

And it stopped working again.. And I have just no idea why... Also the read isn't working either... 然后它又停止了工作..我不知道为什么...而且读取也不起作用...

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;
}
       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>
                              ";

i have also experience that.. hehe try to echo find where you are. 我也有经历..呵呵尝试回声找到你在哪里。 or use my codes .. the post is the name of textbox.. tab 或使用我的代码..帖子是文本框的名称..选项卡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM