entri3 Posted August 14, 2008 Share Posted August 14, 2008 My forums allows the user to input blank sections like blank username,pass,email can someone fix the code below or tell me how to work it i just started php and this is really my first script if (isset($_POST['submit'])) { // Form has been submitted. $errors = array(); // perform validations on the form data $required_fields = array('username', 'password','email'); $errors = array_merge($errors, check_required_fields($required_fields, $_POST)); $fields_with_lengths = array('username' => 30, 'password' => 30,'email' =>30); $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST)); $username = trim(mysql_prep($_POST['username'])); $password = trim(mysql_prep($_POST['password'])); $hashed_password = sha1($password); $email = trim (mysql_prep($_POST['email'])); if ( empty($errors) ) { $query = "INSERT INTO ausers ( username, hashed_password ,email ) VALUES ( '{$username}', '{$hashed_password}', '{$email}' )"; $result = mysql_query($query, $connection); if ($result) { $message = "Welcome To JobLance $username" ; } else { $message = "The user could not be created."; $message .= "<br />" . mysql_error(); } } else { if (count($errors) == 1) { $message = "There was 1 error in the form."; } else { $message = "There were " . count($errors) . " errors in the form."; } } } else { // Form has not been submitted. $username = ""; $password = ""; $email = ""; } ?> <h2>Create New User</h2> <?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?> <?php if (!empty($errors)) { display_errors($errors); } ?> <form action="myfirst.php" method="post"> Link to comment https://forums.phpfreaks.com/topic/119755-forms-allowing-blank-entries/ Share on other sites More sharing options...
ajclarkson Posted August 15, 2008 Share Posted August 15, 2008 Hi, You have done a lot of the groundwork for it as you already are making use of the trim function on all of your input. The logic of what you need to do is check all of your inputs to see if they match your criteria, if they do then you insert the data, if they dont then notify the user. I will show you a quick way around this then you can merge it with your errors array later if you like. After you use the trim functions try something like: if (!strlen($username) == 0) { // all your database code goes here }else{ echo "<p>You must enter data into all fields.</p>"; } Thats a very basic idea, but hopefully it will get you on the right track for how you can check all of them! Hope it has helped ajclarkson Link to comment https://forums.phpfreaks.com/topic/119755-forms-allowing-blank-entries/#findComment-617248 Share on other sites More sharing options...
Fadion Posted August 15, 2008 Share Posted August 15, 2008 Adding to the previous post, u may also consider not to let usersnames or passwords have whitespaces. Not that its a problem, just a bit more aesthetic and less confusing for the actual users. Basically: <?php $username = trim($_POST['username']); if($username != '' && !strstr('$username', ' ')){ //the rest of the code } else{ echo 'The username is empty or contains spaces.'; } ?> Link to comment https://forums.phpfreaks.com/topic/119755-forms-allowing-blank-entries/#findComment-617256 Share on other sites More sharing options...
ajclarkson Posted August 15, 2008 Share Posted August 15, 2008 Very valid point, I have missed this out on a login system I am writing at the minute as well! Thanks! Link to comment https://forums.phpfreaks.com/topic/119755-forms-allowing-blank-entries/#findComment-617264 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.