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() {
    session_start();
    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)",
        $field1, 
        $field2, 
        $field3
    ));

    if ($flag) {
        echo "<script>";
        echo " alert('Data saved successfully');
            window.location.href='".site_url('http://xxx/xxx')."';
            </script>";
        exit();
    }

    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 ?

David

1 Answers

0
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