Wordpress - Custom form that store input in database

I got the problem solution myself. See the code below this will do that.

Put the code inside your newly created custom template.

<?php
    if (!empty($_POST)) {
        global $wpdb;
        $table = wp_achord;
        $data = array(
            'name' => $_POST['yourname'],
            'chord'    => $_POST['chord']
        );
        $format = array(
            '%s',
            '%s'
        );
        $success=$wpdb->insert( $table, $data, $format );
        if($success){
            echo 'data has been save' ; 
        }
    } else {
        ?>
        <form method="post">
            <input type="text" name="yourname">
            <textarea name="chord"></textarea>
            <input type="submit">
        </form>
        <?php 
    }  
?>

My suggestion:

Use Contact Form 7 plus Contact Form DB. You can "design" your form in the backend and putting in the frontend via a simple shortcode.

The Contact Form DB extension let you put your data in the database and offers to you shortcodes to display it (or you can query the data directly from the database if you prefer)


I am going to use this solution, but it seams to be very customize and can that be a problem with themes and upgrades?

  • Link to solution

Tags:

Database

Forms