stewart715 Posted October 19, 2006 Share Posted October 19, 2006 I have a form that when submitted, the below happens. However, If there is already a User ID Number (UID) in the database (say the user decides to update their profile), I would like it to update the Privacy ID (PID) instead of inserting a new row with the same UID but the new PID. What would I put to update it but if a UID doesn't exist, to create it?Thanks in advance, this would be a major major help.[code]$link = mysql_connect("localhost","DATABASENAME","DATABASEPASSWORD");mysql_select_db("TABLENAME",$link);$query="INSERT into TABLENAME (PID,UID) values ('".$PID."','".$UID."')";mysql_query($query);[/code] Quote Link to comment Share on other sites More sharing options...
fert Posted October 19, 2006 Share Posted October 19, 2006 [code]UPDATE tablename SET PID=new_pid[/code] Quote Link to comment Share on other sites More sharing options...
redarrow Posted October 19, 2006 Share Posted October 19, 2006 [code]$query="UPDATE tablename SET PID=PID where UID='$UID'";[/code] Quote Link to comment Share on other sites More sharing options...
stewart715 Posted October 19, 2006 Author Share Posted October 19, 2006 Hmm thanks..i added that above the original $query..but all that does is prevent a duplicate entry in the database..so if i chose 2 as a pid and then 1 as a pid..i cant have that again..but i can still have duplicate usernames..example: mysql tableUID PID1 21 1it wont write 1 2 or 1 1 again..but i want it to just update the PID Quote Link to comment Share on other sites More sharing options...
trq Posted October 19, 2006 Share Posted October 19, 2006 [code=php:0]$link = mysql_connect("localhost","DATABASENAME","DATABASEPASSWORD");mysql_select_db("TABLENAME",$link);$query = "SELECT UID FROM TABLENAME WHERE UID = '$UID'";if ($result = mysql_query($query)) if (mysql_num_rows($result) > 0) { $query = "UPDATE TABLENAME SET PID = '$PID' WHERE UID = '$UID'"; if (mysql_query($query)) { echo "UID updated" } } else { $query="INSERT into TABLENAME (PID,UID) values ('$PID','$UID')"; if (mysql_query($query)) { echo "UID added"; } }}[/code] Quote Link to comment Share on other sites More sharing options...
stewart715 Posted October 19, 2006 Author Share Posted October 19, 2006 :( it runs but no data is recorded :( i've talked to so many programmers...this seems so easy..why cna't i get it to work....this is the actualy code:[code]<?phpinclude("global.inc.php");$errors=0;$error="The following errors occured while processing your form input.<ul>";pt_register('POST','Selectprivacymode');pt_register('POST','Useridnumber');if($errors==1) echo $error;else{$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));$message="Selectprivacymode: ".$Selectprivacymode."Useridnumber: ".$Useridnumber."";$link = mysql_connect("localhost","thepdcom_popnew","PASSWORD");mysql_select_db("thepdcom_popnew",$link);$query = "SELECT uid FROM privacymode WHERE uid = '$Useridnumber'";if ($result = mysql_query($query)) if (mysql_num_rows($result) > 0) { $query = "UPDATE privacymode SET privacyid = '$Selectprivacymode' WHERE uid = '$Useridnumber'"; if (mysql_query($query)) { echo "uid updated";} else { $query="INSERT into privacymode (privacyid,uid) values ('$Selectprivacymode','$Useridnumber')"; if (mysql_query($query)) { echo "uid added"; } } }header("Refresh: 0;url=http://url.com");}?>[/code] 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.