Jump to content

How do I update a MySQL database?


stewart715

Recommended Posts

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]
Link to comment
Share on other sites

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 table

UID    PID
1        2
1        1


it wont write 1  2 or  1  1 again..but i want it to just update the PID
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

:( 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]<?php
include("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]
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.