Jump to content


Photo

How do I update a MySQL database?


  • Please log in to reply
5 replies to this topic

#1 stewart715

stewart715
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 19 October 2006 - 03:24 AM

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.

$link = mysql_connect("localhost","DATABASENAME","DATABASEPASSWORD");
mysql_select_db("TABLENAME",$link);
$query="INSERT into TABLENAME (PID,UID) values ('".$PID."','".$UID."')";
mysql_query($query);


#2 fert

fert
  • Members
  • PipPipPip
  • Advanced Member
  • 1,114 posts

Posted 19 October 2006 - 04:01 AM

UPDATE tablename SET PID=new_pid


Si hoc legere scis nimium eruditionis habes
Gentoo Linux 2007.0 Firefox 2

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 19 October 2006 - 04:07 AM

$query="UPDATE tablename SET PID=PID where UID='$UID'";

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 stewart715

stewart715
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 19 October 2006 - 04:53 AM

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

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 19 October 2006 - 05:10 AM

$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";
    }
  }
}


#6 stewart715

stewart715
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 19 October 2006 - 05:32 AM

:( 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:
<?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");
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users