In fact looking at the code, perhaps I need to explain it a bit better:
Top part:
$id = $_SESSION['username'];
$query = "select * from users where username='$id'";
//now we pass the query to the database
$result=mysql_query($query) or die("Could not get data.".mysql_error());
//get the first (and only) row from the result
$row = mysql_fetch_array($result, MYSQL_ASSOC);
//now finally create variables with data we have got which will be used later
//we will set the value attribute of the appropriate form element to the value from the database
$username=$row['username'];
$password=$row['password'];
$first_name=$row['first_name'];
$maiden_name=$row['maiden_name'];
$last_name=$row['last_name'];
$address_line1=$row['address_line1'];
$address_line2=$row['address_line2'];
$town=$row['town'];
$county=$row['county'];
$postcode=$row['postcode'];
$daytime_phone=$row['daytime_phone'];
$mobile_phone=$row['mobile_phone'];
$evening_phone=$row['evening_phone'];
Here I am retrieving the users details and setting them up so I can echo the results later in the html part of this page.
The next part is to then submit back the changes to the database:
if(isset($_POST["submit"])){
field_validator("username", $_POST["username"], "alphanumeric", 4, 15);
field_validator("password", $_POST["password"], "string", 4, 10);
field_validator("confirmation password", $_POST["password2"], "string", 4, 10);
field_validator("first_name", $_POST["first_name"], "alphanumeric", 1, 15);
field_validator("maiden_name", $_POST["maiden_name"], "alphanumeric", 1, 15);
field_validator("last_name", $_POST["last_name"], "alphanumeric", 1, 15);
field_validator("address_line1", $_POST["address_line1"], "alphanumeric", 4, 15);
field_validator("address_line2", $_POST["address_line2"], "alphanumeric", 4, 15);
field_validator("town", $_POST["town"], "alphanumeric", 4, 15);
field_validator("postcode", $_POST["postcode"], "alphanumeric", 4, 9);
field_validator("daytime_phone", $_POST["daytime_phone"], "number", 1, 11);
field_validator("mobile_phone", $_POST["mobile_phone"], "number", 1, 11);
field_validator("evening_phone", $_POST["evening_phone"], "number", 1, 11);
}
$query = "UPDATE `users`
SET `first_name` = '$first_name',
`maiden_name` = '$maiden_name',
`last_name` = '$last_name',
`address_line1` = '$address_line1',
`address_line2` = '$address_line2',
`town` = '$town',
`county` = '$county',
`postcode` = '$postcode',
`daytime_phone` = '$daytime_phone',
`mobile_phone` = '$mobile_phone',
`evening_phone` = '$evening_phone'
WHERE `username` = '". mysql_real_escape_string($_SESSION['username']). "'
LIMIT 1";
$result = mysql_query($query, $link) or die('Update failed: ' . mysql_error());
if(mysql_affected_rows($link) == 0)
{
echo 'The record was not updated.';
}
else
{
echo 'Record updated successfully...';
}
Now we have the html:
</HEAD>
<html>
<head>
<title><?php print $title ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?php doCSS()?>
</head>
<body>
<form name="form1" method="post" action="<?=$_SERVER["PHP_SELF"]?>" <table>
<p>Username:
<?php echo $username; ?>
</p>
<p>First Name:
<input type="text" name="first_name" value="<?php echo $first_name; ?>">
</p>
<p>Maiden Name:
<input type="text" name="maiden_name" size="10" value="<?php echo $maiden_name; ?>">
</p>
etc, etc
So far I have the isset function working on the field validator, but I cannot see how I can check to see if the form is submitted before I am retrieving the results from the database...