Help Needed- user Input Validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

I used "replace", still it doesn't do any good for phone validation. Except this all other validations are working fine. Now I have come across one more problem.
During registration process, if I miss something, it specifies the mistake. But when I correct it and submit it again, the correct value is not getting recorded in DB saying that a duplicate value exists. All other half filled information is in DB except for the missed ones.

Please ......please help me figure this one out.

Priya
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

How to insert only if all entered information is correct? If something is not right, it should make the user correct it and then it should be stored in the DB. Can anybody please help me solve this problem?

Thank you.
Priya
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

Please help me solve my insert problem. I'm new to IT. Can't figure the problem. Please help me.

Thank you.
Priya
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Help Needed- user Input Validation

Post by McInfo »

phphelpseeker wrote:How to insert only if all entered information is correct? If something is not right, it should make the user correct it and then it should be stored in the DB. Can anybody please help me solve this problem?
PHP Manual: Control Structures

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 2:42 pm, edited 1 time in total.
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

So do you mean to say that I should use an if ..else statemant checking for correctness of all user input and then insert the data? Can you please guide me on this one? how to do that? I can check whether all the fields are set using isset or empty functions. But how can I make sure all the entered data are in the correct form to insert all at onece? Please suggest the correct way of doing it.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Help Needed- user Input Validation

Post by McInfo »

Use a counter to keep track of how many validations fail. If none of them fail, it is safe to run the query.

An example:

Code: Select all

<?php
//* Bad Data
$test['a'] = 'alpha';
$test['b'] = 'bonanza';
$test['c'] = 'jabber';
//*/
 
/* Good Data
$test['a'] = 'alpha';
$test['b'] = 'pop';
$test['c'] = 'requisition';
//*/
 
// The sanitized "good" data
$sanitized = array();
 
// Counter to track the number of failed validations
$failed_validations = 0;
 
// A must be 'alpha'
if ($test['a'] == 'alpha') {
    $sanitized['a'] = $test['a'];
} else {
    $failed_validations++;
}
 
// B must be less than 4 chars long
if (strlen($test['b']) < 4) {
    $sanitized['b'] = $test['b'];
} else {
    $failed_validations++;
}
 
// C's first letter must be 'r'
if (substr($test['c'], 0, 1) == 'r') {
    $sanitized['c'] = $test['c'];
} else {
    $failed_validations++;
}
 
if ($failed_validations === 0) {
    echo "That's the stuff. ";
    echo 'A: '.$sanitized['a'].' ';
    echo 'B: '.$sanitized['b'].' ';
    echo 'C: '.$sanitized['c'].' ';
} else {
    echo 'This is some bad $#!+';
}
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 2:43 pm, edited 1 time in total.
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

Thank you so much. That really helps.

Priya
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

I used die() function instead for each failed validations. Is that a better one?

Priya
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Help Needed- user Input Validation

Post by McInfo »

Use die() when debugging code. For production sites, it should be avoided.

This topic might be helpful:
Related topic: Add link back to page after Validation fails

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 2:45 pm, edited 1 time in total.
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

Thank you.

Priya
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

Form registration validation is working fine now. New challenge:

After the user gets registered or logged in he/she should have access to a password protected directory of their own. It is their own directory that is protected by their username and password. How do I do that? Please help me.

Thank you.
Priya
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

Code: Select all

if (strlen($_POST['username']) >= 6 && strlen($_POST['username']) <=32){
$sql = "SELECT * FROM Accounts WHERE Username='mysql_real_escape_string($username)'";
$query = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($query)  < 1){
$username = $_POST['username']; 
}else{
echo "Username exists. Please enter a different username.<br/>";
die();
}
}
 
The above is my code to check username. It checks whether username exists. It displays this message when it finds an existing username, "Duplicate entry 'miley12' for key 2" instead of the error message I want to be dispalyed. Please help me solve this problem.

Thank you.
Priya
Last edited by Benjamin on Tue May 05, 2009 1:34 pm, edited 1 time in total.
Reason: Changed code type from text to php.
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

I'm doing only server side validation. I don't want to do client side validation. If user enters incorrect data, it shows the error message except for username validation. When user clicks back button to correct the form, how can I change the incorrect form field, for example to red, so that the user can easily identify the incorrect form field. I want to do this using PHP. Can anybody please tell me how to do this? I couldn't find anything online. Somebody please help me.

Thank you.
Priya
phphelpseeker
Forum Commoner
Posts: 30
Joined: Fri May 01, 2009 3:19 pm

Re: Help Needed- user Input Validation

Post by phphelpseeker »

Can anybody please help me figure out how to change the color of the incorrect form filed when the form is redisplayed to the user to correct the input data? Please help me.

Thank you.
Priya
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Help Needed- user Input Validation

Post by McInfo »

This is a two-for-one example. If the input is invalid, the text is initially displayed in red. When the user clicks on or tabs into the textbox, the text changes to the default color (black).

Code: Select all

<html>
<head>
<title>Conditional Style Example</title>
<style type="text/css">
input.valid {
    color: #000;
    }
input.invalid {
    color: #F00;
    }
</style>
</head>
<body>
<?php
$inputClass = 'valid'; // default class
if (true) { // true = invalid input, false = valid input
    $inputClass = 'invalid';
}
?>
<input type="text" class="<?php echo $inputClass; ?>" value="example" onfocus="this.className='valid';" />
</body>
</html>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 2:45 pm, edited 1 time in total.
Post Reply