vincmeister
vincmeister

Reputation: 191

wordpress custom form to database

I'm trying to create a custom submit form on wordpress I'm using the wp page template from the twentyfourteen theme and put my code inside of the

<div id="content" class="site-content" role="main">

$wpdb->insert('wp_table',array (
                    'db_column' => $inputtext,
                    )
                );

It works, but every time the page loads, it also submit blank data into my wp_table. how to avoid this? I just want to input data using the submit button.

Upvotes: 1

Views: 202

Answers (3)

Valentin Mercier
Valentin Mercier

Reputation: 5326

Before submitting your data, you must make sure it is not empty. This is the right way to go:

if(!empty($_POST['datakey'])) {
    $wpdb->insert('wp_table',array (
                'db_column' => $inputtext,
                )
            );
}

This code snippet is commonly seen in Wordpress plugins or themes.

Upvotes: 0

Sonimy
Sonimy

Reputation: 19

if( count($_POST) ){
         $wpdb->insert('wp_table',array (
                'db_column' => $inputtext,
                )
            );

}

Upvotes: 2

Avinash Babu
Avinash Babu

Reputation: 6252

You can avoid submitting blank data into the wordpress table by using isset in php

if(!empty($_POST['streamname'])) {

// the code
}

See this one ..Hope it was helpful

Upvotes: 3

Related Questions