I have a custom HTML form in WordPress which inserts data into a custom table. I have written code in the functions.php file to insert data.

The HTML code is as below:

<form id="regForm" method="POST" action="">

My PHP function is as below:

function xx_data_insert() {
    require_once "wp-load.php";
    require_once "dbconfig.php";
    global $wpdb, $current_user;

    $current_user = wp_get_current_user();
    $table_name = 'xx_table';

    //Form variables defined too many to add here
    //insert statement
    $flag = $wpdb->query( $wpdb->prepare(
        (field1,field2,field3) VALUES(%s,%s,%s)",

    if ($flag) {
        echo "<script>";
        echo " alert('Data saved successfully');

    if( isset($_POST['submit']) ) xx_data_insert();

When the submit button is clicked, there is no error. The page just refreshes. How do I check if the code is even going to the if statement? Any way to trap ?


1 Answers

Amit Bariya On

$flag = $wpdb->query( $wpdb->prepare( (field1,field2,field3) VALUES(%s,%s,%s)", $field1, $field2, $field3 ));

check this (field1,field2,field3) VALUES(%s,%s,%s)", starting quote is missing