Inserting data into a table in WordPress database using WordPress $wpdb

后端 未结 4 1938
名媛妹妹
名媛妹妹 2021-02-04 07:46

I am starting out plugin development and have followed the tutorials on the WordPress Codex sites. I am now stuck - I have a database called \"wp_imlisteningto\", where the

相关标签:
4条回答
  • 2021-02-04 08:32

    You've probably figured this out by now, but no one addressed it here. Your sample code has '$s' in the 3rd parameter (2nd array), but that should be '%s' because it's for value-formatting. The WP Codex says [http://codex.wordpress.org/Class_Reference/wpdb] that this format parameter for $wpdb->insert() is optional.

    0 讨论(0)
  • 2021-02-04 08:34

    Try this..

    <?php
    global $wpdb;
    
    $wpdb->insert( $table_name, array( 'album' => "$_POST['album']", 'artist' => "$_POST['artist']" ) );
    ?>
    

    Ex :

    <?php
    global $wpdb;
    
    $wpdb->insert($table_name , array('chart_name' => "Line Chart" ,'chart_type' => "trends",'status' => 0));
    
    ?>
    
    0 讨论(0)
  • 2021-02-04 08:41

    I think there are 2 mistakes in you sql string.

    Think it should be the $table_name variable should be concatenated

    $sql = "CREATE TABLE" . $table_name . "(
    id mediumint(9) AUTO_INCREMENT,
    album VARCHAR(50),
    artist VARCHAR(50),
    PRIMARY  KEY (id)
    )";
    

    and remove ; on the last line.

    0 讨论(0)
  • 2021-02-04 08:44

    including

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

    worked for me

    0 讨论(0)
提交回复
热议问题