简体   繁体   English

比较两个表,同时使用php mysql将数据插入数据库

[英]comparing two tables while inserting data into database using php mysql

category --> `cat_id`,`name`
sub_category --> `sub_cat_id`,`name`
category_subcategory_association --> `cat_sub_cat_id`,`cat_id`,`sub_cat_id`
order_master --> `order_id`,`cat_sub_cat_id`,`cat_id`,`sub_cat_id`,`order_name`

These are the database tables which iam having while adding the orders into order_master table if we give cat_id,sub_cat_id while inserting into the table it should check the whether these ids are present in the association table or not,if the id is present in association table then we should get that association id and should insert into the orders table. 这些是iam在将订单添加到order_master表中时所拥有的数据库表,如果我们在插入表时给出cat_id,sub_cat_id,则应检查关联表中是否存在这些ID,如果关联表中存在ID那么我们应该获取该关联ID,并将其插入订单表中。

How can we do this can anyone help me 我们该怎么做,谁能帮助我

Thanks in advance. 提前致谢。

You just need to check if it exists, if not - insert it into the the db and fetch the newest id. 您只需要检查它是否存在(如果不存在)-将其插入db并获取最新的ID。

IE: IE浏览器:

$cat_name = ; // your code
$sub_cat_name =; // your code

$query = mysql_query("SELECT cat_id FROM `category` AS `c` WHERE name = '$cat_name'");

if(!mysql_num_rows($query)) { 
  mysql_query("INSERT INTO category (name) VALUES ('$cat_name')");
  $cat_id = mysql_insert_id();
} else 
    $cat_id = mysql_fetch_array($query);

// now do the same for the sub category and then insert it into to order_master

this is a general code, you need to customize it into your framework's standards. 这是一个通用代码,您需要将其自定义为框架的标准。

hope it helped 希望能有所帮助

you implement the checks via a select statement: http://dev.mysql.com/doc/refman/5.1/de/select.html 您可以通过选择语句实现检查: http : //dev.mysql.com/doc/refman/5.1/de/select.html

running multiple sql queries, eg. 运行多个SQL查询,例如。 a mixture of select and update statements requires you some knowledge on: 选择和更新语句的混合需要您具备以下知识:

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

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