Support » Developing with WordPress » Database INSERT not working

  • I’m either updating or inserting data in a table depending on whether it exists or not. The update works fine as expected, but the insert is only half working.

    I explode the input name to help with organizing the data so it looks something like this:

    name=”relation,type,state”

    That will produce:

    
    // relation is what I use to identify the relationship between text and url
    // type will either be 'text' or 'url'
    // state is a two character abbreviation ex: 'ca'
    $key = array('relation','type','state');
    

    Then I use a foreach on the fields and have the insert:

    
    if($key[1] === 'text'){
        $wpdb->insert(
            'areas_module_text',
            array(
                'text'  => $value,
                'state'  => $key[2],
                'relation' => $key[0]
            ));
    }else if($key[1] === 'url'){
        $wpdb->insert(
            'areas_module_urls',
            array(
                'url'  => $value,
                'state'  => $key[2],
                'relation' => $key[0]
            ));
    }
    

    Link to full script that gets executed: https://pastebin.com/kfmH9yB4

    The first if is working, but the second appears to just get bypassed for some reason. The code block is almost identical to the update except for the WHERE and both work fine. It’s just the second part of the INSERT that is not. Why is that?

    I swapped the 2 inserts and the url one still didn’t work. I removed the check to see if the entries exist and the updates section and it worked, but I obviously can’t do that because it just keeps inserting new rows every time. I also dumped the data of $key for each input and the data appears correct so I don’t understand why it isn’t working.

    Hopefully somebody can explain why this is happening.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Suggest that you turn on debugging, details here:
    https://codex.wordpress.org/Debugging_in_WordPress

    Also capture the return value of the “$wpdb->insert” calls and display the result.

    Thread Starter stinkysGTI

    (@stinkysgti)

    I turned on debugging and fixed a couple of unrelated errors, but ultimately nothing changed. It’s as if the if($key[1] === 'url') condition is returning false for some reason which is weird because the previous one, if($key[1] === 'text'), works just fine.

    I tried switching it to just a generic else with no condition and it still isn’t working.

    Thread Starter stinkysGTI

    (@stinkysgti)

    I ended up just separating them out and it works. Not as pretty or efficient, but it works

    Here is another approach, the SQL INSERT clause has a variation “ON DUPLICATE KEY UPDATE”
    details here:
    https://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html

    I use this when I had a page streaming form updates back to the server using AJAX, and the specific form row was part of the key, the context did not differentiate when it was the first data for that row or when the javascript sensed a revision to the contents and queued the rows data to be posted to the database again.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Database INSERT not working’ is closed to new replies.