简体   繁体   中英

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.

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.

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

running multiple sql queries, eg. a mixture of select and update statements requires you some knowledge on:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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