Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //add db conn script
- include 'resource/utlities.php';
- include 'resource/database.php';
- //process the form
- if (isset($_POST['signupBtn'])){
- //initialize the array to store any message
- $form_errors = array();
- //form validation
- $required_fields = array('email','username','password');
- // empty field
- $form_errors = array_merge($form_errors, check_empty_fields($required_fields));
- //min length
- $field_to_check_length = array('username' => 4, 'password' => 6);
- //merge min len with form err
- $form_errors = array_merge($form_errors, check_min_length($field_to_check_length));
- //email val merging
- $form_errors = array_merge($form_errors, check_email($_POST));
- //check if error array is empty
- if (empty($form_errors)){
- //collect form data and store in variables
- $email = $_POST['email'];
- $username = $_POST['username'];
- $password = $_POST['password'];
- //encrypt passwor
- $hash_password = password_hash($password, PASSWORD_DEFAULT);
- try{
- //sql insert stmt..
- $sqlInsert = "INSERT INTO my_db.user2(username, password, email,join_date) VALUES(:username,:password,:email,now())";
- //use pdo prepare sanitize data
- $statement = $db->prepare($sqlInsert);
- $statement->execute(array(':username'=>$username, ':email'=>$email, ':password'=>$password));
- if($statement->rowCount() == 1){
- $result = "<p style = 'padding: 20px; color: green;'>Registration Successful</p>";
- }
- }catch(PDOException $ex){
- $result = "<p style = 'padding: 20px; color: red;'>Registration Not Successful ".$ex->getMessage()."</p>";
- }
- }else{
- if (count($form_errors) == 1){
- $result = "<p style = 'color: red;'>There is 1 error in the form<br>";
- }else{
- $result = "<p style = 'color: red;'>There were ".count($form_errors)." an error in the form<br>";
- }
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head lang = "en">
- <meta charset = "UTF-8">
- <title>Register page</title>
- </head>
- <body>
- <h2>Homepage</h2><br>
- </body>
- <h3>Register Form</h3>
- <?php if(isset($result)){
- echo $result; }
- ?>
- <?php if (!empty($form_errors)){
- echo show_errors($form_errors);
- }
- ?>
- <form method = "post" action = "">
- <table>
- <tr>
- <td>Email: </td>
- <td><input type = "text" value = "" name = "email" placeholder="email"></td>
- </tr>
- <tr>
- <td>Username: </td>
- <td><input type = "text" value = "" name = "username" placeholder="username"></td>
- </tr>
- <tr>
- <td>Password: </td>
- <td><input type = "password" value = "" name = "password" placeholder="password"></td>
- </tr>
- <tr><td></td><td><input style = "float:right;" type = "submit" name = "signupBtn" value = "Signup"></td></tr>
- </table>
- </form>
- <p><a href = "index.php">Back</a></p>
- </html>
Add Comment
Please, Sign In to add comment