Kartikeya Garg
Kartikeya Garg

Reputation: 79

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:

 public function storeID($cat_id,$user_id){

    $stmt = $this->conn->query("SET FOREIGN_KEY_CHECKS=0");
    $stmt= $this->conn->query("INSERT INTO `user_category` (`user_id`, `cat_id`) VALUES (`$user_id`, `$cat_id`)");
    $result = $stmt->execute();

    $stmt->close();

  }

Is there any solution, please refer.

Upvotes: 1

Views: 133

Answers (1)

nospor
nospor

Reputation: 4220

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

Upvotes: 1

Related Questions