How to Save Your Form Data in WordPress Database

Does a question arise How to Save Your Form Data in WordPress Database? , Developer Guidance helps to Saving data from the form to WordPress Database Table. Here we not use any plugin to store form data into a wordpress database table.

Saving custom WordPress form data into database we use $_POST.
here is a syntax you can use <?php if ( isset( $_POST[‘submit’] ) ){ }?>
or <?php if (!empty($_POST)) {}?>

Above $_POST is a super global variable which is used to collect form data after submitting. This is widely used variable.

Use the below code by which you can save the custom WordPress form data into database table.

[php]if (!empty($_POST)) {
        global $wpdb;
        $table = table_name;
        $data = array(
            'name' => $_POST['name'],
            'email' => $_POST['email'],
            'mobile' => $_POST['mobile'],
            'message' => $_POST['message'],
        );
        $format = array(
            '%s',
            '%s',
            '%s',
            '%s'
        );
        $success=$wpdb->insert( $table, $data, $format );
        if($success){
            echo 'data has been save' ; 
        }
    } else {
        [/php]
        <form method="post" action="" id="form1">
                <input name="name" type="text">
                <input name="email" type="email">
                <input name="mobile" type="number">
                <input name="message" type="text">
                <input type="submit" name="submit" value="send">
</form>
       [php]
    }  
[/php]

The above code helps you how to send form data to a database in WordPress. Insert a row into a table in WordPress. <?php $success=$wpdb->insert( $table, $data, $format ); ?>.

table
(string) The name of the table to insert data into.

data
(array) Data to insert (in column => value pairs). Both $data columns and $data values should be “raw” (neither should be SQL escaped).

format
(array|string) (optional) An array of formats to be mapped to each of the values in $data. If string, that format will be used for all of the values in $data. If omitted, all values in $data will be treated as strings unless otherwise specified in wpdb::$field_types.

By above help you How can I insert data from custom form in a WordPress site?