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.

Related posts

Leave a Reply

3 comments

  1. 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

  2. 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.