genista Posted September 12, 2006 Share Posted September 12, 2006 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 Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 12, 2006 Share Posted September 12, 2006 echo out the update see whats wrong ok. Quote Link to comment Share on other sites More sharing options...
genista Posted September 12, 2006 Author Share Posted September 12, 2006 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? Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 12, 2006 Share Posted September 12, 2006 are try to check if the name is charecters or numbers. Quote Link to comment Share on other sites More sharing options...
genista Posted September 12, 2006 Author Share Posted September 12, 2006 sorry dont understand... Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 12, 2006 Share Posted September 12, 2006 look up eregi okheres an example ok.[code]<?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>";}?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.