• 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 wp_ was inserted using:

    $table_name = $wpdb->prefix . "imlisteningto";

    When the plugin is activated.

    The database itself has three columns, set up when the plugin is activated:

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

    I am trying to insert data (by creating a new row) into this database from a php form.

    Within the WordPress admin, I create a new page which has the very simple form:

    <form action="/wp-content/plugins/listeningto/formhtml.php" method="post">
    Album: <input type="text" name="album" />
    Artist: <input type="text" name="artist" />
    <input type="submit">
    </form>

    Which as you can see calls formhtml.php, which is:

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

    When I submit the form, I get an Error 500.0 when running the plugin in Worpdress on IIS7.0, and a “Page Not Found” when running on another web server which runs apache.

    If I change formhtml.php to:

    <?php
    
    echo $_POST['album'];
    echo $_POST['artist'];
    
    ?>

    Works fine – I get the album and artist that I put in the form. Obviously something I’m doing wrong when inserting the data (in a new row) into the database.

    Then, during testing, I did this – if I update formhtml.php with this (include the config file):

    <?php
    require_once('../../../wp-config.php');
    $table_name = $wpdb->prefix . "imlisteningto";
    $wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ), array( '$s', '$s' ) );
    ?>

    I no longer get an error message, but data still doesn’t get put into the database.

    I also tried

    <?php
    require_once('../../../wp-config.php');
    global $wpdb;
    $table_name = $wpdb->prefix . "imlisteningto";
    $wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ), array( '$s', '$s' ) );
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • UPDATE – I got it working by doing this:

    <?php
    require_once('../../../wp-config.php');
    global $wpdb;
    $table_name = $wpdb->prefix . "imlisteningto";
    $wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ) );
    ?>

    I.e. I need to include wp-config.php. If I drop that, it breaks.

    Hi Matty,

    Thanks for your update, the problem seems that “array( ‘$s’, ‘$s’ )” should not be inserted into the $table_name.

    Very helpful.

    Hi Globalfish,

    I also found that

    <?php
    require_once('../../../wp-load.php');
    global $wpdb;
    $table_name = $wpdb->prefix . "imlisteningto";
    $wpdb->insert( $table_name, array( 'album' => $_POST['album'], 'artist' => $_POST['artist'] ) );
    ?>

    Also worked, but yes I forgot to mention that $format as described here also caused problems. With that dropped, it worked.

    Hi Matty,

    Yes, both

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

    work.
    And require_once(...) is very critical, without this line, the insert does not work! will get an error.

    I tried another code, which also worked.

    <?php
    require_once('/Applications/XAMPP/xamppfiles/htdocs/wordpress/wp-config.php');
    // change to your own directory if necessary
    global $wpdb;
    $table = $wpdb->prefix."main_table"
    $wpdb->query("INSERT INTO $table (album, artist) VALUES ($_POST['album'], $_POST['artist'])");
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Creating a PHP form to insert data into a table’ is closed to new replies.