cobusbo Posted August 23, 2015 Share Posted August 23, 2015 Hi I'm trying to compare two tables with each other and update the 2nd table with the new updated name from table 1 via their unique identical ID $resultus = mysql_query("SELECT * FROM Users2"); $rowus = mysql_fetch_array($resultus); $naamus = urldecode($rowus['Username']); $mxitus = $rowus['mxitid']; $resultnup = mysql_query("SELECT * FROM pm"); $rownup = mysql_fetch_array($resultnup); $naamup = $rownup['username']; $ipup = $rownup['ip']; while($ipup == $mxitus){ $updatename = "UPDATE pm SET username= \"$naamus\" WHERE ip = \"$mxitus\""; mysql_query($updatename) or die("Error: ".mysql_error()); } The $mxitus and $ipup is the 2 fields that would be identical in both tables now Im trying to update the pm table username field with the naamus field from Users 2 any assistance please? Quote Link to comment https://forums.phpfreaks.com/topic/297910-compare-tables-and-update-2nd-table-with-new-value/ Share on other sites More sharing options...
Barand Posted August 23, 2015 Share Posted August 23, 2015 UPDATE pm INNER JOIN Users2 u ON pm u.mxitid = pm.ip SET pm.username = u.username Quote Link to comment https://forums.phpfreaks.com/topic/297910-compare-tables-and-update-2nd-table-with-new-value/#findComment-1519519 Share on other sites More sharing options...
cobusbo Posted August 24, 2015 Author Share Posted August 24, 2015 UPDATE pm INNER JOIN Users2 u ON pm u.mxitid = pm.ip SET pm.username = u.username I tried it $updatename = "UPDATE pm INNER JOIN Users2 u ON pm u.mxitid = pm.ip SET pm.username = u.Username"; mysql_query($updatename) or die("Error: ".mysql_error()); getting error Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'u.mxitid = pm.ip SET pm.username = u.Username' at line 2 Quote Link to comment https://forums.phpfreaks.com/topic/297910-compare-tables-and-update-2nd-table-with-new-value/#findComment-1519525 Share on other sites More sharing options...
Solution Barand Posted August 24, 2015 Solution Share Posted August 24, 2015 Oops! typo. Remove the "pm" after "ON" UPDATE pmINNER JOIN Users2 u ON pm u.mxitid = pm.ipSET pm.username = u.username 1 Quote Link to comment https://forums.phpfreaks.com/topic/297910-compare-tables-and-update-2nd-table-with-new-value/#findComment-1519529 Share on other sites More sharing options...
cobusbo Posted August 24, 2015 Author Share Posted August 24, 2015 Oops! typo. Remove the "pm" after "ON" UPDATE pm INNER JOIN Users2 u ON pm u.mxitid = pm.ip SET pm.username = u.username Thank you that solved the problem just learned something new with this query Quote Link to comment https://forums.phpfreaks.com/topic/297910-compare-tables-and-update-2nd-table-with-new-value/#findComment-1519531 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.