Jump to content

guide on validating this form please


xcoderx

Recommended Posts

ok i need help and tips on validating the fields. could someone show me how i go on about?

 

here is the form.php

<?php
$id 	= $_REQUEST['uid'];
$conn 	= db_connect();
$sql  	= "SELECT * FROM users WHERE id='".$id."'";
$result = mysql_query($sql, $conn) or die(mysql_error());  
$user_profile = mysql_fetch_array($result);
echo '<form action="update.php" method="post">';
echo '<font color="blue">Username:</font> <br/><input type="text" name="user_name" value="'.$user_profile['user_name'].'"> <br/><br/>';
echo '<font color="blue">First Name:</font> <br/><input type="text" name="f_name" value="'.$user_profile['f_name'].'"> <br/><br/>';
echo '<font color="blue">Last Name:</font> <br/><input type="text" name="l_name" value="'.$user_profile['l_name'].'"> <br/><br/>';
echo '<font color="blue">E-mail:</font><br/> <input type="text" name="email" value="'.$user_profile['email'].'"> <br/><br/>';
echo '<font color="blue">DOB:</font> <br/><input type="text" name="date" value="'.$user_profile['date'].'"> <br/><br/>';
echo '<input type="submit" name="submit" value="Update">';
echo '<input type="hidden" name="user_id" value="'.$user_profile['id'].'">';
echo '</form>';
?>
and here is the update.php which i must validate

<?php
session_start();
include 'db.inc.php';
//print_r($_POST);
$conn = db_connect();


$sql =" UPDATE users SET
                        user_name ='".$_POST['user_name']."',
                        f_name ='".$_POST['f_name']."',
                        l_name ='".$_POST['l_name']."',
                        email ='".$_POST['email']."',
                        date ='".$_POST['date']."'
                    WHERE id ='".$_POST['user_id']."' ";
mysql_query($sql,$conn);

header("Location:edit_a_user.php?uid=".$_POST['user_id']);
                    
//print $sql;
                        
?>

Link to comment
https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/
Share on other sites

This is how I would do it:

 

$sql = sprintf("UPDATE users SET user_name ='%s', f_name = '%s', l_name = '%s', email = '%s', date = '%s', WHERE id = %d ",
	mysql_real_escape_string($_POST['user_name']),
	mysql_real_escape_string($_POST['f_name']),
	mysql_real_escape_string($_POST['l_name']),
	mysql_real_escape_string($_POST['email']),
	mysql_real_escape_string($_POST['email']),
	mysql_real_escape_string($_POST['date']),
	$_POST['user_id']
	);

There's a lot of ways you could do that. If you're familiar with regex, you could just use that to verify the data. Otherwise, you could just use simple functions such as these:

 

strlen($string) to get the length of the string.

is_numeric(string) to check if the string is a number (for the id).

 

 

Also, to make the DOB field more consistent, I would do something like this:

if ($dob = strtotime($_POST['date'])) {
$dob = date('d/m/Y', $dob);
} else {
// Can't process the date.
}

well i just need to do it in a simpiliar  way something like this mebbe

 

if ($_POST['submit]==1) { 

    $errormsg = "";

    if ($_POST[user_name]){ 

        $user_name = $_POST[user_name];

else{ 

        $errormsg = "Please enter Username"; 

    } 

 

but im not getting the idea on how do i use to display error message on  the form.php if any field left empty and data not get updated till fields are filled

 

if( $_POST['submit'] == 1 ) isn't going to work for you, since there is no such value coming from that form. You have a hidden field in the form that will also need to be validated upon submission, so that could/should be used to see if the form has been submitted also. The if(isset($_POST['submit'])) method can be an issue with some browsers (especially Internet Exploder) that don't handle submit buttons properly.

Make sure the hidden user_id field is not empty when trim()'d and rtrim()'d, and at the same time validate it for the correct data type. Is the user_id field always expected to be an integer? If so, cast it as an integer type. Once that's handled, go on about validating the rest of the fields' values. Store any errors in an array so you can check for them, and echo them out later. The database insert should not be allowed to proceed unless the errors array is empty. If you need more help, or an example, just say so.

The code for checking the null value is as follows 

<form method="post">
<input type="text" name="id">
<input type="submit" name="submit">
</form>

<?php
if(isset($_POST['submit'])) {
$v=$_POST['id'];
if(count($v)<=0) {
echo 'The field cannot be left blank';
}
else {
//whatever you want to do

}

}


?>


I think the above idea could help you with your problem

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.