Advanced Search

Results 1 to 2 of 2

Thread: Update is deleting existing record

  1. #1
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Update is deleting existing record

    I am experiencing a problem when I attempt to edit a record in a Student Record database in PHPMyAdmin. When the user wants to update a record they click an update button which brings them to a screen with the current field values with an input type text option to change the field value. However when the user clicks on submit the code only updates the amended field value and deletes the other values. The following is the code I am using.

    Code:
    <?php
    
    	if(isset($_POST['subEdit'])) {
    	$subEdit = $_POST['subEdit'];
    	} if(!empty($subEdit)){
    	$surname= $_POST['surname'];
    	}  else{
    	$surname= $surname;
    	}
    
            if(isset($_POST['subEdit'])) {
    	$subEdit = $_POST['subEdit'];
    	} if(!empty($subEdit)){
    	$firstName= $_POST['firstName'];
    	}  else{
    	$firstName= $firstName;
    	}
    
    {
    $sql ="UPDATE personal_details SET surname='$surname', firstName='$firstName'  WHERE userName='$userName'";
    
    if (!mysql_query($sql))
      {
      die('Could not connect: ' . mysql_error());
      }
    }   
    
    ?>
    Does anybody have any suggestions as to how I can alleviate this issue?

    Thanks
    Last edited by james438; 04-30-2013 at 02:11 PM. Reason: format

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,681
    Thanks
    78
    Thanked 89 Times in 87 Posts

    Default

    It sounds like the values are not all being passed from your form page.

    Add this:

    Code:
    echo "$sql";exit();
    and place it right after this:

    Code:
    $sql ="UPDATE personal_details SET surname='$surname', firstName='$firstName'  WHERE userName='$userName'";
    If a field value like $_POST['firstName'] is being passed from the form page and $_POST['surname'] is not then your query will update the firstName with "Fred" and update surname with the empty value that it is. In this case it will delete the field value for surname.
    Last edited by james438; 05-01-2013 at 11:42 PM. Reason: fixed an error in my last paragraph.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    anton2511 (05-01-2013)

Similar Threads

  1. Replies: 1
    Last Post: 04-22-2011, 03:36 AM
  2. update record when another is selected from lookup
    By sniperman in forum MySQL and other databases
    Replies: 0
    Last Post: 01-21-2011, 03:42 AM
  3. Replies: 7
    Last Post: 11-20-2008, 08:57 AM
  4. DHTML Modal window v1.1-How to load record per ID for update form
    By kashifmalik in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 11-19-2008, 06:13 PM
  5. create / insert / update record(s)
    By boogyman in forum MySQL and other databases
    Replies: 0
    Last Post: 08-10-2007, 07:24 PM

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
  •