WordPress.org

Ready to get started?Download WordPress

Forums

$wpdb->insert isn't working for me... (7 posts)

  1. jlknauff
    Member
    Posted 3 years ago #

    I'm using the following code:

    $wpdb->insert('wp_network_members', $insert_member_data);

    an example value for $insert_member_data is:

    Array
    (
        [f_name] => Joe
        [l_name] => Smith
        [title] => CEO
        [company_name] => Acme Inc.
        [company_description] => Something about us
        [join_date] => 2011-03-17
    )

    Is there a reason this isn't working? If it makes a difference, manually typing the array into the line as below works fine, but with quite a few fields, it becomes cumbersome.

    $wpdb->insert( 'wp_network_members', array( 'f_name' => 'Joe'));

  2. jlknauff
    Member
    Posted 3 years ago #

    Also, it mentions on the page http://codex.wordpress.org/Function_Reference/wpdb_Class that you shouldn't SQL escape the data

    (array) Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).

    but since this data is coming from a form, wouldn't that be dangerous?

  3. Tim Moore
    Moderator
    Posted 3 years ago #

    $wpdb->insert automatically escapes the data before inserting.

    It is possible some of your array keys (like "title") are causing reserved name issues with MySQL. I'd check that before anything else, as I find that is usually the issue when $wpdb->insert doesn't work.

  4. jlknauff
    Member
    Posted 3 years ago #

    As far as I can tell, none of the array keys are reserved words with MySQL

    [f_name]
    [l_name]
    [title]
    [company_name]
    [company_description]
    [countdown]
    [rss]
    [twitter]
    [facebook]
    [linkedin]
    [email]
    [phone]
    [fax]
    [address_1]
    [address_2]
    [city]
    [state]
    [zip]
    [photo]
    [logo]
    [join_date]

    I even added a prefix (netmemb_) to each field and updated the code accordingly, but the row still won't insert.

    Is there anything else I should be looking for?

  5. Tim Moore
    Moderator
    Posted 3 years ago #

    Look into this: http://codex.wordpress.org/Function_Reference/wpdb_Class#Show_and_Hide_SQL_Errors

    Try to use the WordPress debugging to capture the SQL error. That's about the only way to figure out what is going on.

  6. jlknauff
    Member
    Posted 3 years ago #

    Found the problem and it works perfectly now! Thanks :)

  7. grimmdude
    Member
    Posted 3 years ago #

    how did you get it to work? having the same problem...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags