xcoderx Posted July 11, 2010 Share Posted July 11, 2010 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 More sharing options...
xcasio Posted July 11, 2010 Share Posted July 11, 2010 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'] ); Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084386 Share on other sites More sharing options...
xcoderx Posted July 11, 2010 Author Share Posted July 11, 2010 thats i know bro. but before updating i must check if any empty fields then data should not get updated. im not getting the idea how to. Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084387 Share on other sites More sharing options...
xcasio Posted July 11, 2010 Share Posted July 11, 2010 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. } Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084389 Share on other sites More sharing options...
xcoderx Posted July 11, 2010 Author Share Posted July 11, 2010 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 Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084395 Share on other sites More sharing options...
Pikachu2000 Posted July 11, 2010 Share Posted July 11, 2010 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. Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084443 Share on other sites More sharing options...
xcoderx Posted July 11, 2010 Author Share Posted July 11, 2010 whats the solution? could help me please? Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084449 Share on other sites More sharing options...
Pikachu2000 Posted July 11, 2010 Share Posted July 11, 2010 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. Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084470 Share on other sites More sharing options...
xcoderx Posted July 11, 2010 Author Share Posted July 11, 2010 yes bro needed example. Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084495 Share on other sites More sharing options...
FURQAN Posted July 11, 2010 Share Posted July 11, 2010 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 Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084521 Share on other sites More sharing options...
xcoderx Posted July 12, 2010 Author Share Posted July 12, 2010 its not working. Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084801 Share on other sites More sharing options...
Pikachu2000 Posted July 12, 2010 Share Posted July 12, 2010 What do you mean by not working? Link to comment https://forums.phpfreaks.com/topic/207407-guide-on-validating-this-form-please/#findComment-1084811 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.