【发布时间】:2012-03-28 02:33:48
【问题描述】:
我有两张桌子: p_model: (id auto inc), (name), (p_category_id) AND p_category: (id), (name)
基本上每个模型都与一个类别相关联。我需要生成一个插入新模型并添加与之关联的 p_category 的 ID 的 INSERT 语句。
我的 PHP 函数同时传递两个值:
function Perform_Insert_Model($model_name, $category_name)
{
$statement = "INSERT INTO p_model(`name`,`p_category_id`) VALUES ('$model_name','??????')";
//Where the "??????" is I need a statement that searches for $category_name (string) in the p_category table and returns the id.
....
}
对该函数的示例调用是 $database->Perform_Insert_Model("Banana","Fruit")。 Banana 将是一个新的产品模型,并具有水果 ID 的外键关联。
【问题讨论】:
标签: php sql insert foreign-keys