SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Deadwood, SD USA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Updating account database

    Hi Forum Users,
    I'm making a page for my site users to update there personal info. When I tested the form it updated any fields that I filled in but the fields I left blank it deleted what was in the database.
    This is the code I am using to insert the new data.

    $Phone=$_POST['phone'];
    $Address1=$_POST['address1'];
    $Address2=$_POST['address2'];
    $City=$_POST['city'];
    $State=$_POST['state'];
    $Zip=$_POST['zip'];
    $Country=$_POST['country'];

    mysql_query("UPDATE user SET Email='$Email', Phone='$Phone', Address1='$Address1', Address2='$Address2', City='$City', State='$State', Zip='$Zip', Country='$Country' WHERE UserID = '$Userid'");

    What can I do so that if there is field with nothing to post that it will leave the current data in tact?

    Thanks for your help.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Dakotakid50 View Post
    What can I do so that if there is field with nothing to post that it will leave the current data in tact?
    use php to inspect the form fields, and if there's nothing in it, leave it out of the SQL
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Deadwood, SD USA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am some what new to php and I don't know the code to use to check to see if the entry in the form is blank or not.

  4. #4
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Update to your code:

    PHP Code:
    $Phone=mysql_real_escape_string($_POST['phone']);
    $Address1=mysql_real_escape_string($_POST['address1']);
    $Address2=mysql_real_escape_string($_POST['address2']);
    $City=mysql_real_escape_string($_POST['city']);
    $State=mysql_real_escape_string($_POST['state']);
    $Zip=mysql_real_escape_string($_POST['zip']);
    $Country=mysql_real_escape_string($_POST['country']); 

    if(empty(
    $phone)){
    echo 
    "Please fill up phone field";
    }
    elseif(empty(
    $Address1)){
    echo 
    "Please fill up Address field";
    }
    elseif(...and 
    so on

  5. #5
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Deadwood, SD USA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •