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.
- The topic ‘Database INSERT not working’ is closed to new replies.