Jump to content

Field Validation Problems


genista

Recommended Posts

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:

[code=php:0]
$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'];
[/code]

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:
[code=php:0]
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.';
    }
[/code]

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
Link to comment
https://forums.phpfreaks.com/topic/20489-field-validation-problems/
Share on other sites

Ok, I have just done some testing. The following code gives me an undefined variable on first_name and maiden_name and the query is unsuccessful:

[code=php:0]
f(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"];
}[/code]

However, if I change the above to:

[code=php:0]
if(isset($_POST["submit"])){
$first_name=$_POST["first_name"];
$maiden_name=$_POST["maiden_name"];
}
[/code]

It updates fine, but notice that I do not have field validation at this stage which is ultimately what I want, therefore what would be the best way of using the field validator function here?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.