Jump to content

Archived

This topic is now archived and is closed to further replies.

stewart715

How do I update a MySQL database?

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]

Share this post


Link to post
Share on other sites
[code]
$query="UPDATE tablename SET PID=PID where UID='$UID'";
[/code]

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.