php - Check if an id exists in a table before adding it to another table -
i'm doing small thing like
feature see on facebook. way i'm doing this. have table called products
contains products people can like
. (stripped down):
id | prodname | status (0=clear, 1=blocked) ---------------------------------------------------------- 1 | philips food processor | 0 2 | le sharp knife | 0 3 | ye cool fridge | 0 comes `likes` table this: id | prodname | prodid | userid -------------------------------------------- 1 | philips food processor | 1 | 1 2 | le sharp knife | 2 | 1 3 | ye cool fridge | 3 | 1 4 | ye cool fridge | 3 | 2
i need check, before adding likes
table, if product id actually exists in products table , status = 0. lot of php code. way using sql? possible? using foreign keys or that?
i'm using innodb table type.
you can conditional insert
. product 6 , user 7:
insert likes (prodname, prodid, userid) select prodname , id , 7 products id = 6 , status = 0
if inserts no rows, know product did not exist status 0.
Comments
Post a Comment