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:

   $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”…

Read More

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…)

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…

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…

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

Related posts

Leave a Reply

1 comment

  1.        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