I am hoping the community can give me a little insight into what is not working with my code, I am following a Udemy course. I have followed the accompanying video which developed an undefined variable error, which after doing some research I believe I have fixed by declaring variables as empty strings being able to be over-ridden by the form data.
The form sends data to the database if both are completed, and if one of the fields is empty then it doesn’t, which is as it should be.
If one of the fields is empty it should return a statement asking the user to enter data into the respective field, but nothing is being sent.
The only difference between the tutorial and my code is I have used the materialize framework, where the tutorial used bootstrap, but I can’t see that being the issue.
I have attached my code, and commented out redundant parts.
<?php include('php/connection.php'); //validates data for create user form if( isset( $_POST["createUserBtn"])){ $createUsername = ""; $createUserPassword = ""; function validateFormData( $formData ) { $formData = trim( stripcslashes( htmlspecialchars( $formData))); return $formData; } if( !$_POST["createUsername"]){ $createUsernameError = "Enter a username <br>"; } else { $createUsername = validateFormData( $_POST["createUsername"]); } if( !$_POST["createUserPassword"]){ $createUserPasswordError = "Enter a Password <br>"; } else { $createUserPassword = validateFormData( $_POST["createUserPassword"]); } if( $createUsername && $createUserPassword) { $query = "INSERT INTO users (user_id, userName, userPassword) VALUES (NULL, '$createUsername', '$createUserPassword')"; // if( mysqli_query( $connection, $query)){ // echo "New User added"; // } else { // echo "Error: ".$query."<br>".mysqli_error($connection); // } } } ?> <!DOCTYPE html> <html lang="en"> <?php require('static/header.php'); ?> <?php $createUsernameError = ""; $createUserPasswordError = ""; ?> <div class="col s8 m8 l5 valign-wrapper"> <div class="container"> <form action="<?php echo htmlspecialchars( $_SERVER["PHP_SELF"] ); ?>" method="post"> <div class="row"> <div class="col s12"> <span><h4>Create your user account - create user.php</h4></span> <div class="row form-font"> <div class="col s12"> <div class="input-field"> <a class="red-text"><?php echo $createUsernameError; ?></a> <input placeholder="Enter your username" type="text" name="createUsername"> <label for="email">Username</label> </div> <div class="input-field"> <a class="red-text"><?php echo $createUserPasswordError; ?></a> <input placeholder="Enter your password" type="password" name="createUserPassword"> <label for="password">Password</label> </div> <div class="row left-align"> <div class="col s2"></div> <div class="col s8"> <button class="btn-flat waves-effect waves-custom" type="submit" name="createUserBtn"><i class="material-icons left">create</i>Create Account</button> </div> <div class="col s2"></div> </div> </div> </div> </div> </div> </form> </div> </div> <?php require('static/footer.php'); ?> </html>
Advertisement
Answer
Look carefully at your code and the places where you make use of – for example – the $createUsernameError
variable.
If there’s an error, you set a message in it with this line: $createUsernameError = "Enter a username <br>";
. Great, just what you wanted.
However, later on in the code, you run $createUsernameError = "";
, which resets it to empty again. And that happens in all circumstances, whether an error was identified or not. And it happens before you try to echo
that variable onto the page.
So basically you’re setting the value and then immediately blanking it again before you output it. You need to make sure it’s only set blank in situations where there’s no error. It’s the same problem for the password error message.
An easy way to do that would simply be to set the value blank before you run the error checks. Then it’ll stay blank if there’s no error, but it won’t overwrite any error messages which do get set.
So just move these lines:
$createUsernameError = ""; $createUserPasswordError = "";
to the top of your script.
P.S. Please pay attention to the security warnings posted in the comments and urgently fix your code to remove these vulnerabilities before using this code in any kind of live environment. Even if you don’t plan to use this code for real, you should still fix these issues so that you learn to do things the correct, safe, reliable way and don’t get into bad habits. If you copied this code from a course online, I suggest finding a better course.