I am executing a query which is as follows.
global $wpdb;
$table_name = $wpdb->prefix . 'prousers';
$results = $wpdb->get_results("INSERT into $table_name
(name,gender,mailid,empid,address,phone)
VALUES(
'".$InputName."','".$gender."','".$InputEmail."',
'".$emp_id."','".$address."','".$phn_num."'
)", OBJECT );
Everything is going well. I just wanted to validate it, so I did following:
if($results) {
echo'success';
} else {
echo "error";
}
Values are correctly getting inserted into the table, but it is showing a fail message (it prints "error").
Why is this?
I suggest you to use $wpdb->insert( $table, $data, $format );
Then you will get details in $wpdb->insert_id to write condition.
You can use die function with get_results() method,
global $wpdb;
$table_name = $wpdb->prefix . 'prousers';
$results = $wpdb->get_results( "INSERT into $table_name
(name,gender,mailid,empid,address,phone)
VALUES('".$InputName."','".$gender."','".$InputEmail."',
'".$emp_id."','".$address."','".$phn_num."')", OBJECT ) or die('Error while query run');
You need to $wpdb->insert();
instead of $wpdb->get_result();
as:
$wpdb->insert( "INSERT into $table_name (name,gender,mailid,empid,address,phone) VALUES('".$InputName."','".$gender."','".$InputEmail."', '".$emp_id."','".$address."','".$phn_num."')", OBJECT );
And right after this use this for getting last Id:
$lastid = $wpdb->insert_id;
And check either query execute or not as:
if($lastid > 0) {
echo'success';
}
else {
echo "error";
}