In your code I encountered two problems.
- First one is you have not defined
$current_user
and$abcde
variable. For using global$current_user
you need to define it in the top of the function asglobal $current_user
. Otherwise it will not get the$current_user
variable. And also for getting the current user ID yo need to use$current_user->ID
after declaring the global variable. And for$abcde
variable please declare or store the value first in that variable. - And second one is you are checking logic with
if (!$alibaba)
which actually means if the$alibaba
variable is false. Obviously if the DB entry is successful the$alibaba
will return true. So the loop will not be executed.
So now your whole code will be like below-
function insert_form() {
global $wpdb, $current_user;
$table_name = $wpdb->prefix . "food_resarved";
$abcde=""; // Later some where put a value in it. Otherwise it will not work.
$alibaba = $wpdb->insert(
$table_name,
array(
'id' => '',
'user_id' => wp_get_current_user()->ID,
'name' => wp_get_current_user()->user_firstname,
'lastname' => wp_get_current_user()->user_lastname,
'post_title' => $_POST['post_title'],
'food_selected' => $_POST['food'],
'guest_number' => $_POST['guest_food'],
'email' => wp_get_current_user()->user_email
),
array( '%d', '%s', '%s' , '%s' , '%s' , '%s' , '%s', '%s' )
);
if ( $alibaba ) {
$mylink = $wpdb->get_results( "select * from wp_food_resarved where user_id like '".$current_user->ID."' AND post_title like '".$abcde."' ");
foreach ($mylink as $posta){
echo '<div class="ordered-food">';
echo ' شما ';
echo $posta->food_selected;
echo ' سفارش دادهاید ';
echo '</div>';
}
}
else {
echo "Die";
}
}
Hope that helps you.