jimmyoneshot Posted April 26, 2010 Share Posted April 26, 2010 I'm having a total nightmare getting my code to work to the point that I'm thinking of quitting I think I would actually pay good money for someone to give me an answer on this problem I'm that desperate. Please :'( I use the following statement to insert all records into a table 'userlinks' from another table 'links' for the user with the logged in userid ($param_userid) I want to edit it so that if these records exist for that user it will simply update them with the info from the corresponding records in the links table and if any records exist in the links table that the user hasn't added they will be added for that specific user to the userlinks table. So basically to insert if a specfic record/records doesn't exist in the userlinks table that does in the links table AND to update the existing ones that do exist with the corresponding info from the links table, all for the logged in user with $param_userid. $sql = "INSERT INTO userlinks( userid, linkid, categoryid, label, icon, url, favourited ) SELECT '$param_userid', linkid, categoryid, label, icon, url, 'N' FROM links"; Can anyone please help my sanity by taking a few minutes just to help me out with this please Quote Link to comment Share on other sites More sharing options...
freelance84 Posted April 26, 2010 Share Posted April 26, 2010 I'm not sure exactly what you meant. But can you use: http://www.w3schools.com/sql/sql_update.asp Quote Link to comment Share on other sites More sharing options...
jimmyoneshot Posted April 26, 2010 Author Share Posted April 26, 2010 Hi. I've tried this but it doesn't seem to work and it just inserts all the records over and over again. I think it's ignoring the update part at the start:- $sql = "UPDATE userlinks SET userid, linkid, categoryid, label, icon, url, favourited SELECT '$param_userid', linkid, categoryid, label, icon, url, 'N' FROM links WHERE userid = '$param_userid'"; $result = mysql_query($sql); if (mysql_affected_rows($result) == 0) { $sql = "INSERT INTO userlinks( userid, linkid, categoryid, label, icon, url, favourited ) SELECT '$param_userid', linkid, categoryid, label, icon, url, 'N' FROM links"; $result = mysql_query($sql); } What I'm trying to do is insert/udate the records in the userlinks table FROM the ones in the links table. I've seen examples of updating using values defined in the php code itself but I need it to get the values from the userlinks table and update the same values in the links table or add them in if they don't exist. Quote Link to comment Share on other sites More sharing options...
jimmyoneshot Posted April 27, 2010 Author Share Posted April 27, 2010 Anyone got anything more that can help me with this? Quote Link to comment 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.