简体   繁体   English

使用php使用外键将值插入数据库

[英]Insert values into database with foreign key using php

I am having trouble with the following issue: I have two tables: 我遇到以下问题:我有两个表:

category table:
category_id     |      category_name

brand table
brand_id     |      brand_name     |      category_id (FOREIGN KEY)

I was trying to insert a new brand name value using PHP with FOREIGN KEY that referenced to category id . 我试图使用带有引用category id FOREIGN KEY的PHP插入新的品牌名称值。

$insert = "INSERT INTO brand (category_id, brand_name) VALUES('$category_name','$brand')"; 

However it could not be added into the brand table. 但是它无法添加到品牌表中。

Foreign key means category_id in table brand is refer to the same category_id in table category, so the category_id inserted in table brand must exist in table category. 外键表示表品牌中的category_id是表类别中相同的category_id,因此表格中插入的category_id必须存在于表类别中。

When you insert new record in table brand, you need to insert the ID (not the category name) and make sure that id is exist in table category 在表品牌中插入新记录时,需要插入ID(不是类别名称)并确保表中存在id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM