Hi,
I have a function that validates html data before it can be passed to the database. This works fine when a user first registers on the site, but when trying to apply it to a form where a user can update their details is proving to be difficult. When I try and overwrite a field that has the field validator function on it, it returns empty and my update query does not work - no errors on that.
The first part of this code displays getting the data from the database, I have cut a lot of the fields out to keep this post as short as possible:
$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);
$first_name=$row['first_name'];
$maiden_name=$row['maiden_name'];
So far so good, this works fine, the next part is to set the variables and validate them before sending them back to the database:
if(isset($_POST["submit"])){
field_validator("first_name", $_POST["first_name"], "alphanumeric", 1, 15);
$town=field_validator("maiden_name", $_POST["maiden_name"], "alphanumeric", 4, 15);
}
if(empty($messages)) {
$first_name=$_POST["first_name"];
$maiden_name=$_POST["maiden_name"];
}
$query = "UPDATE `users`
SET `first_name` = '$first_name',
`maiden_name` = '$maiden_name'
WHERE `username` = '". mysql_real_escape_string($_SESSION['username']). "'
LIMIT 1";
$result = mysql_query($query, $link) or die('Update failed: ' . mysql_error());
echo $query;
print_r($query);
mysql_info($link) ;
if(mysql_affected_rows($link) == 0)
{
//$link next to affected rows and mysql query
echo 'The record was not updated.';
}
So far the code above returns a "the record was not updated", if you want the field validator function posting please let me know.
Thanks,
G