Support » Plugins » Hacks » $wpdb->insert isn't working for me…

  • 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'));

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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?

    $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.

    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?

    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.

    Found the problem and it works perfectly now! Thanks 🙂

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘$wpdb->insert isn't working for me…’ is closed to new replies.