-1

I have two tables, user_info and Friend_info. I want to do that when user update his record in user_info then it should also b update in friend_info where friend_id=user_id. I have tried this

UPDATE user_info (name, user_email, Gender, DOB, contact, address) WHERE user_id='$user_id',
                 friends_info(name, user_email, Gender, DOB, contact, address) WHERE friend_id='$user_id'
                  values('$name', '$user_email', '$Gender', '$DOB', '$contact', '$address');

But its not working . Any other solution please. It'll be appreciated.

I know this question is too late to ask now a days but its my problem because i am confused after doing so many search and no query is working in my case.

Sina R.
  • 1,781
  • 2
  • 19
  • 37
bebo
  • 73
  • 7
  • 1
    Why you are trying to update these two table at single query ?Try instead two update query . – Web Artisan May 11 '16 at 05:24
  • because when user update his record in user_info table then it should be updated in friends_info also by executing one php file from android studio @BikashP – bebo May 11 '16 at 05:26
  • Then it should be run by executing two query also.Just write two query line by line. – Web Artisan May 11 '16 at 05:36
  • You have a comma at the end of the first statement. This needs to be a semicolon ; But this is also not a nice way to write code. You want to run it in 2 different queries so if one fails you know the reason. If you do a search on "mysql transactions" you can also group the queries so if one fails the other change is removed. – Brett May 11 '16 at 05:41
  • @bebo -so is your problem solved? if yes you may accept the answer by clicking on a tick icon left side of answer you liked. – Sina R. May 11 '16 at 06:20
  • i will do this but first i am trying to solve my problem .. that is not solved yet @ncm – bebo May 11 '16 at 06:24

2 Answers2

1

Your question is not clear. So are you testing something like you want query in phpmyadmin. if not then you might need to do in as a transaction. but if its a test or such try this:

UPDATE user_info (name, user_email, Gender, DOB, contact, address) 
values('$name', '$user_email', '$Gender', '$DOB', '$contact', '$address') 
WHERE user_id='$user_id';
UPDATE friends_info(name, user_email, Gender, DOB, contact, address) 
values('$name', '$user_email', '$Gender', '$DOB', '$contact', '$address') 
WHERE friend_id='$user_id';

So this is two query which then they are gonna execute together. But now in PHP

check these:

https://stackoverflow.com/a/802474/2226796

http://se2.php.net/manual/en/mysqli.multi-query.php

PHP + MySQL transactions examples

Community
  • 1
  • 1
Sina R.
  • 1,781
  • 2
  • 19
  • 37
  • no its not working here i tried like this $sql_query = "UPDATE user_info (name, user_email, Gender, DOB, contact, address) values('$name', '$user_email', '$Gender', '$DOB', '$contact', '$address') WHERE user_id='$user_id'"; "UPDATE friends_info(name, user_email, Gender, DOB, contact, address) values('$name', '$user_email', '$Gender', '$DOB', '$contact', '$address') WHERE friend_id='$user_id'"; if(mysqli_query($con,$sql_query)) { echo "

    success

    "; }
    – bebo May 11 '16 at 05:42
  • can u please explain it more? for example if query is working then its reply with success otherwise not . thanks – bebo May 11 '16 at 06:00
  • @bebo I updated my answer. You need also to see why you want to execute two queries together. check third link for transaction. – Sina R. May 11 '16 at 06:03
0

You can join the two tables in your statement using user_id as the joining key.



    UPDATE user_info ui 
    INNER JOIN friends_info fi 
    ON ui.user_id = fi.user_id
    SET ui.name = $name,
    SET ui.user_email = $email,
    SET ui.Gender = $Gender,
    SET ui.DOB = $DOB,
    SET ui.contact = $contact,
    SET ui.address = $address,

    -- set friends_info

    SET fi.name = $name,
    SET fi.user_email = $email,
    SET fi.Gender = $Gender,
    SET fi.DOB = $DOB,
    SET fi.contact = $contact,
    SET fi.address = $address
    WHERE ui.user_id = $user_id;

Adrian
  • 442
  • 4
  • 15