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
Share on other sites

Why don't you select what is in table 2 id 4 and put the result into variables then update the second table using those variables

 

UPDATE tbl1 SET (id='$id', name='$name', address='$address', phone='$phone', pin='$pin', email='$email') WHERE id = 3 LIMIT 1

Link to comment
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

 

 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.