data from form on page added to database

I am using update_option to add Google API JSON results to database. I have that working and able to generate a map with get_option. What I would like to do is:
each time an address is submitted with the form on any given page-to have that saved instead of overwriting the previous entry.

  • Ash
    • WordPress Hacker

    Hello @Paul

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    You can use add_option to add each time. But in this case you need new option name every time. And interesting thing is, if you use new option name every time, you can use update_option too.

    Update_option updates the value if option_name exists, if not it will add the data.

    http://codex.wordpress.org/Function_Reference/add_option
    http://codex.wordpress.org/Function_Reference/update_option

    Please let me know if you find this is helpful, or if you have any further questions.

    Cheers
    Ash

  • Paul
    • Design Lord, Child of Thor

    here is a snippet of the code:

    $address = esc_html( $_POST['address'] );
    
    //assign JSON object to var using geocodeing function('address') input
    $address_results = boj_gmap_geocode($address);
    
    // transfer data to options array
    $options['py_name'] = $py_username;
    $options['address'] = $address;
    $options['coords']  = $address_results;
    
    // create and name the option_name and set fields with $options array
    update_option( 'py_menu_item', $options );
  • Ash
    • WordPress Hacker

    You need to change py_menu_item every time. The value will be saved based on py_menu_item so it will always overwrite if the name is same.

    Where are you implementing this? For a logged in user? If yes, then you can use user_id to save new data.

    like:

    $current = get_current_user();
    update_option( 'py_menu_item' . $current->user_login, $options );

    And then you can get the data by:

    $current = get_current_user();
    get_option( 'py_menu_item' . $current->user_login );

    Please let me know if you find this is helpful, or if you have any further questions.

    Cheers
    Ash

  • Paul
    • Design Lord, Child of Thor

    No, not a logged in user. It is just a form on user-facing-page that anyone can use (my preference to do it that way).
    Anytime any random person (to be secured with Captcha) comes to the site and enters an address, the address is to be saved so that it can later be called in search.

  • Ash
    • WordPress Hacker

    What I suggested is
    get_option( 'py_menu_item' . $current->user_login );
    is similar to the stackoverflow solution
    update_option( 'py_menu_item_' . sanitize_key( $py_username ), $options );
    He suggested sanitize_key( $py_username ) instead of $current->user_login. I suggested $current->user_login because I thought the page is for logged in user only. The logic is to add a unique keyword with py_menu_item so that it will not be overwritten!

    Also according to stackoverflow, are you sure $py_username is always unique? If not, then it might overwrite again.

    For getting the data you need to use get_option($key). So if you stored as

    update_option( 'test_key', 'test_value' );

    You need to use

    get_option('test_key');

    IMO, the best solution will be if you create an extra table in the database. So, there can be columns like ID, address, username, address_result etc.

    Hope it helps! Please feel free to ask more question if you have.

    Cheers
    Ash

  • Ash
    • WordPress Hacker

    Some more notes, as stackoverflow is not a private forum and I think many people (may be hundreds or even thousands) are waiting there to answer a question in first place.

    But in our forum we process all the threads based on a queue. So first come, first serve. We are human here and when we stuck in a thread, it needs time, and it takes some time to come back in older threads.

    I believe you won't get any more faster private community than us :slight_smile:

    We are really happy to see you in the community!

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.