What am I trying to do I want to be able to insert records into my custom Wordpress table, but only able to have one row for each unique company name.
If a second record with the same company name is attempted to be inputted, I want the script to bow out gracefully (i.e. not return an error).
What I've done
I am using Wordpress's $wpdb->insert
function to insert new rows into a very basic 3-column table which looks a bit like this:
id | company | name
I am using this code:
foreach ( $this->tlds_to_insert as $tld_to_insert ) {
$wpdb->insert(
'wp2_bnfo_allowedtlds',
array(
'company'=> $this->member_company,
'name' => $tld_to_insert,
),
array(
'%s',
'%s'
)
);
}
Table set up
I've set up so both the ID (primary key) and the company as unique indexes because I only want one unique record per company.
What I am struggling with
The issue I am facing is that if I try and input a duplicate entry, it does fail (that's good) but I get an error notice saying that is a duplicate and I want to avoid this failure notice.
Research I have read a number of StackOverflow Posts but these tend to relate to people writing custom SQL outside of Wordpress and I am not sure if / how I can utilise this code.
My best guess so far: I could create a $wpdb->get_results
query to see if the value exists, and then only run the $wpdb->insert
query if it does not. This seems like high overhead.
Any thoughts much appreciated.