PHP add first table column id to another table column id - php

There are 2 Table in the Database.
tbl_second_category
tbl_third_category
I'm already passing
col_first_category_id
from
table1 tbl_second_category
to
Table2 tbl_third_category col_second_category_id
using select box control
You can see id 13 in both the Tables
when I'm passing above id, at the same time how can I pass col_second_category_id from Table1
tbl_second_category to column col_for_delete_row Table2 tbl_third_category.
Thank you

then use trigger, you may need more then one for update and delete events... for example:
CREATE TRIGGER trigger_name AFTER INSERT ON tbl_second_category
FOR EACH ROW
UPDATE tbl_third_category
SET col_for_delete_row = NEW.col_second_category_id
WHERE col_second_category_id = NEW.col_first_category_id;

Related

avoid Duplicate entry in mysql

I have two tables say, Table1 and Table2.
Table1 contain 20 fileds with primary key ID for Table1 auto increment and Table2 contain same 20 column with primary key ID1 for TAble2 auto increment.
Table1- ID,Column1,Column2,Column3....,Colunm20.
Table2-ID1,ID,Column1,Column2,.....,Column20
I insert each record into Table1. I want to check duplicate Entry for Table1 if all field are same.
Table2 contain the Entry form Table1.If I modified record First record goes to Table2 as it is then it will update into Table1.(i.e first copy the record into Table2 for backup and then update into Table1) So, same condition for Duplicate entry check near Table2 also need. Please Help!
I have coded this in php.
You can write trigger for table 1 which is copying data from table2 on every insert event.
And for unique data either you can check it on php side before you insert or you can define column as unique. Your question is bit confusing so Its difficult to give exact answer.
What are the unique columns ??

Copying tables mysql

I am newbie to sql advance and i am trying to insert rows to one table by selecting from another one. But some bug is coming out.
Here is my problem
So i have two tables
table1 having id(autoincrement), names
table2 having id(autoincrement), names
Now at starting table1 is empty and table2 having 2 rows
1,'myself'
2,'yourself'
So the problem starts here
When i execute following query
Insert into table1 (names) select (names) from table2
So now both rows of table2 must be copied to table1
Ya its working fine.
But what about autoincrement value of id?
By till now table1 autoincrement id should store 3 since next row to be inserted should have id 3
But its not working like expected so table1 autoincrement id stores 4 i.e, 1(current id value)+2*(no of rows inserted)-1
So next time when i execute same query it inserts row with id 4. Skips id=3.
This is problem hope you all got what i am talking about.
Thanks for helping in advance.
Try this
INSERT INTO TABLE1 SELECT * FROM TABLE2
Go ahead an copy your data over, and then you can modify the starting auto_inc to whatever you choose.
ALTER TABLE table AUTO_INCREMENT = 1000;
Alter Table will take around 1 sec as it first creates shadow of the table then the query is processed and if the no. of rows of table goes above then it may take around 20-30 sec. so I dont think that alter is good answer for this problem. Hope someone will help you with insert command such as:
Insert into table1 set id = 1; etc...

Insert distinct records in the table while updating the remaining columns

This is actually a form to update the team members who work for a specific client, When i deselect a member then it's status turns to 0.
I have a table with all unique records. table consists of four columns -
first column is `id` which is unique and auto_incremented.
second column is `client_id`.
third column is `member_id`. (these second and third columns together make the primary key.)
fourth column is `current` which shows the status (default is 1.).
Now i have a form which sends the values of client_id and member_id. But this forms also contains the values that are already in the table BUT NOT ALL.
I need a query which
(i) `INSERT` the values that are not already in the table,
(ii) `UPDATE` the `current` column to value `0` which are in the table but not in the form values.
here is a screenshot of my form.
If (select count(*) from yourtable where client_id = and member_id = ) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
First of all check if the value exists in the table or not, by using a SELECT query.
Then check if the result haven't save value so it will be inserted, else show an error .
This would be a great time to create a database stored procedure that flows something like...
select user
if exists update row
else insert new row
stored procedures don't improve transaction times, but they are a great addition to any piece of software.
If this doesn't solve your problem then a database trigger might help out.
Doing a little research on this matter might open up some great ideas!
Add below logic in your SP
If (select count(*) from yourtable where client_id = <value> and member_id = <value>) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
if you want simple solution then follow this:
*) use select with each entry in selected team.
if select returns a row
then use update sql
else
use insert sql.
In your case member_id & client_id together makes the primary key.
So , you can use sql ON DUPLICATE KEY UPDATE Syntax.
Example:
$sql="INSERT INTO table_name SET
client_id='".$clientId."',
member_id='".$member_id."',
current='".$current."'
ON DUPLICATE KEY
UPDATE
current = '".$current."'
";
In this case when member_id & client_id combination repeats , it will automatically executes update query for that particular row.

How to insert a id of a new row into another linking table?

I've a table with the purpose of linking two other tables with foreign keys (table 3).
table1 = id, name
table2 = id, sport
table3 = fk_table1_id, dk_table2_id
I do server side code with php and i need a way to automatic insert a row into table 3 when a row is inserted into table2.
But i dont know how to do a php query that will get the generated id of the new row in table2?(The id of table one i do have stored in a php variable)Im a looking at a smart stored procedure?
Steps to follow:
Insert a row into table2.
Find the generated key from table2 and assign it to a PHP variable.
-- read this into a php variable, say, $last_key
SELECT LAST_INSERT_ID();
Use the $last_key in insert statement for table3.
You can Follow below Steps:
1) Make ID to AUTO_INCREMENT IN Table2
2) Create Store Procedure that work as below:
A) INSERT Value to your Table2 :
B) Increase ID value : SELECT LAST_INSERT_ID() INTO VARIABLE;
C) Insert Value to your Table3 : use VARIABLE to get ID of Table2

MySql insert last id after each insertion

i'm trying to insert some rows from one table to another and add last inserted id to first table:
INSERT INTO tableA (fooA, fooA2) SELECT fooB, fooB2 FROM tableB;
And now i want to add last inserted id into tableB for each row
UPDATE tableB set tableA_id = LAST_INSERT_ID();
But for multiple records it's wrong. Any idea how to update tableB after each insertion into tableA? Is it possible to do with MySql query, or just write some PHP script?
Try to get it done with trigger .

Categories