Jump to content

[SOLVED] Updating MYSQL row using PHP


blogfisher

Recommended Posts

Hello,

  I am using php - mysql to update my database. I have two tables in a database. Both have same fields as below:

 

Tbl1 & Tbl2: id, name, address, phone, pin, email

I want to update row in Tbl1 where id = 3 with row in Tbl2 with id = 4.

I am using below query, however its not working...

 

UPDATE Tbl1 SET (id,name,address,phone, pin, email) WHERE id= 3 SELECT id,name,address,phone, pin, email FROM Tbl2 WHERE id = 4

 

Is this the correct way ?

Link to comment
https://forums.phpfreaks.com/topic/149510-solved-updating-mysql-row-using-php/
Share on other sites

This is completely untested and I don't even think it would work, but.

 

UPDATE tb1 SET (name=(SELECT name FROM tbl2 WHERE id=4), address=(SELECT address FROM tbl2 WHERE id=4), phone=(SELECT phone FROM tbl2 WHERE id=4), pin=(SELECT pin FROM tbl2 WHERE id=4), email=(SELECT email FROM tbl2 WHERE id=4)) WHERE id = 3 LIMIT 1 

 

I'm not sure that it will work, but that's the only way I can think of it working using a query.

 

Edit:

 

Eek, or just follow the link Stephen posted :P

 

 

Hello Stephen ,

  I am not sure how to do it. I tried but still giving error. That example updates entire table so no need to take reference of any id. I changed it in following ways. Each one is giving the same error and not working :

 

1. UPDATE Tbl1 SET ROW = (SELECT FROM Tbl2 WHERE id = 4) WHERE id = 3

2.  UPDATE Tbl1 WHERE id = 3 SET ROW = (SELECT FROM Tbl2 WHERE id = 4)

 

Any error in the statements? or they or not logical ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.