Kartikeya Garg Kartikeya Garg - 1 year ago 67
MySQL Question

Insert data into foreign key column

I have following tables:enter image description here

I want to insert data into user_category.Is there any way to insert data into user_category.user_id reference account.id and user_category.cat_id reference categories.cat_id.

I am doing this method but can't work:

mysqli->prepare("SET FOREIGN_KEY_CHECKS=0");
mysqli->prepare("INSERT INTO `user_category` (`user_id`, `cat_id`)
VALUES(`$user_id`, `$cat_id`)");

Is there any solution, please refer.

Answer Source

prepare() function just only prepares query and does not execute it. In your case you can use query() instead of prepare()