Support » Fixing WordPress » Insert data to the database from a form

  • I currently know how to insert data into the database, using PHP MySQL. However, when I try to do it with $wpdb class, I just do not know how to go about it. What I am looking for, is an example code where it shows the form data and then the code necessary to implement that data into the database table. In my particular case I am using a form where I have:

    Last Name:

    That data, when is captured, I want to take it to my database table, named clients, using code $wpdb. I would truly appreciate an answer to this, hopefully giving example code.

    Thank you very much,

    Ricardo Cerda de Groote

Viewing 4 replies - 1 through 4 (of 4 total)
  • from

    // if using a custom function, you need this
    global $wpdb
    $lastn = 'Last Name';
    $firstn = "First Name';
    $wpdb->insert( 'clients', array( 'last_name' => $lastn, 'first_name' => $firstn ), array( '%s', '%d' ) )

    Ok, I can see that,

    but how do I connect a form, which will be in one of the pages, where the customer will enter the information ??. I mean, that at what moment does the database know I am inserting from the form.

    If you could, let me know more in detail..

    Thank you very much for your first reply,


    Hi Ricardo, it is quite unfortunate the Codex does not contain a real example of form posting, storing, and retrieving to redisplay the form!!
    I recently posted an outline of such as you wish to do, here:

    I think the step you are missing is the form must post to some existing page URL, such as the page itself! Therefore the php code in the page must recognize the incoming post prior to starting to display anything. See if you can make sense of my example.
    You also have to deal with the question of user identity. How do you know who is submitting the form? My example asks for a WordPress login to do this. Hence the data can be retrieved by that specific user in the future, to be updated!


    If you roll your own form, just be sure to properly sanitize any form posted data before it hits the db.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Insert data to the database from a form’ is closed to new replies.