1

I've build a form and inserted its data into custom database tables using $wpdb.

When I click on the submit button, the page reloads and one row is added to the database, but nothing displays on website.

My insert code is:

function insert_form() {

    global $wpdb;
    $table_name = $wpdb->prefix . "food_resarved";
    $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 '".$curent_user."' 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";
    }
}
2

1 Answer 1

2

In your code I encountered two problems.

  1. 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 as global $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.
  2. 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.