• Resolved mjd82

    (@mjd82)


    I am creating a custom table in the WP database. However, after this is created successfully I don’t seem to be able to actually insert a row into it… Am I missing something here?

    // store the specified img URL in the wp db
    if (!empty($_POST['imgURL'])) {
      global $wpdb;
      $table_name = $wpdb->prefix . 'my_custom_table';
    
      if ($wpdb->get_var('SHOW TABLES LIKE "' . $table_name . '"') === $table_name) {
        $delete = $wpdb->query('TRUNCATE TABLE ' . $table_name);
      } else {
        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        $charset_collate = $wpdb->get_charset_collate();
        $create = "CREATE TABLE $table_name (
          url varchar(55) NOT NULL DEFAULT ''
        ) $charset_collate;";
        dbDelta($create);
      }
    
      $wpdb->insert($table_name, array('url' => $_POST['imgURL']));    // why doesn't this work???
      wp_send_json_success(array('msg' => 'Image inserted in database successfully'));
    }
Viewing 1 replies (of 1 total)
  • Thread Starter mjd82

    (@mjd82)

    Increasing the varchar() did the trick in the end.

Viewing 1 replies (of 1 total)

The topic ‘$wpdb->insert does nothing when creating a table programmatically’ is closed to new replies.