Jump to content


Photo

Field Validation Problems


  • Please log in to reply
5 replies to this topic

#1 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 12 September 2006 - 10:48 AM

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

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 September 2006 - 11:07 AM

echo out the update see whats wrong ok.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 12 September 2006 - 11:54 AM

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:

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"];
}

However, if I change the above to:

if(isset($_POST["submit"])){ 
 $first_name=$_POST["first_name"];
$maiden_name=$_POST["maiden_name"]; 
}

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?

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 September 2006 - 11:56 AM

are try to check if the name is charecters or numbers.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 12 September 2006 - 12:06 PM

sorry dont understand...

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 12 September 2006 - 12:09 PM

look up eregi ok

heres an example ok.

<?php

$message1="my name is redarrow";

$message2="687687";


if(eregi("[a-z]",$message1)){

echo "<br>letters<br>";

}

if(eregi("[0-9]",$message2)){

echo "<br>numbers<br>";

}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users