Log in

View Full Version : Update is deleting existing record



anton2511
04-30-2013, 08:06 AM
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.


<?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

james438
04-30-2013, 02:24 PM
It sounds like the values are not all being passed from your form page.

Add this:


echo "$sql";exit();

and place it right after this:


$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.