I have two tables: searches and commonWords. Given user input Q, if Q is not found in commonWords, I want to insert it into searches.
To be honest I’m just completely confused how I would do this. How can I combine INSERT INTO with the whole SELECT .. WHERE thing?
I know I can do it with two queries:
$db->query('SELECT * FROM commonWords WHERE word = ?', $q);
if ($db->affectedRows() > 0)
$db->query('INSERT INTO searches (search) VALUES (?)', $q);
However, I would like to confine this operation to one query.
Thanks for any help!
try this if you want to insert a row that exisists in commonWords
but you have to be carefull because you have to set a unique index for searches.search
if not you could insert multiple rows of the same value
if you want insert a non existing word into searches, then you have to try this