Jump to content

Inserting/Updating Into a MySql table from A Php file


jimmyoneshot

Recommended Posts

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  :D

Link to comment
Share on other sites

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.

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.