Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- $_SESSION['message'] = '';
- $mysqli = new mysqli("localhost", "root", "hotboy123", "accounts");
- //the form has been submitted with post
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- //two passwords are equal to each other
- if ($_POST['password'] == $_POST['confirmpassword']) {
- //set all the post variables
- $username = $mysqli->real_escape_string($_POST['username']);
- $fname = $mysqli->real_escape_string($_POST['fname']);
- $lname = $mysqli->real_escape_string($_POST['lname']);
- $email = $mysqli->real_escape_string($_POST['email']);
- $password = md5($_POST['password']); //md5 has password for security
- //set session variables
- $_SESSION['username'] = $username;
- $sql= "SELECT * FROM users WHERE username = '$username'";
- $result=$mysqli->query($sql);
- if(mysqli_num_rows($result)!=0)
- {
- $_SESSION['message'] = 'Username already exists!';
- }
- else
- {
- // excecute insert query
- //insert user data into database
- $sql = "INSERT INTO users (username, fname, lname, email, password) "
- . "VALUES ('$username','$fname','$lname','$email', '$password')";
- //if the query is successsful, redirect to welcome.php page, done!
- if ($mysqli->query($sql) === true){
- $_SESSION['message'] = "Registration successful! Added $username to the database!";
- header("location: welcome.php");
- }
- else {
- $_SESSION['message'] = 'User could not be added to the database!';
- }
- $mysqli->close();
- }
- }
- else {
- $_SESSION['message'] = 'Two passwords do not match!';
- }
- }
- ?>
- <!-- <link href="//db.onlinewebfonts.com/c/a4e256ed67403c6ad5d43937ed48a77b?family=Core+Sans+N+W01+35+Light" rel="stylesheet" type="text/css"/> -->
- <link rel="stylesheet" href="form.css" type="text/css">
- <div class="body-content">
- <div class="module">
- <!-- <img src="capheus.png"/> -->
- <h1>Create an account</h1>
- <form class="form" action="form.php" method="post" enctype="multipart/form-data" autocomplete="off">
- <div class="alert alert-error"><?= $_SESSION['message'] ?></div>
- <input type="text" placeholder="Username" name="username" required />
- <input type="email" placeholder="Email" name="email" required />
- <input type="text" placeholder="First Name" name="fname" required />
- <input type="text" placeholder="Last Name" name="lname" required />
- <input type="password" placeholder="Password" name="password" autocomplete="new-password" required />
- <input type="password" placeholder="Confirm Password" name="confirmpassword" autocomplete="new-password" required />
- <input type="submit" value="Register" name="register" class="btn btn-block btn-primary" />
- <a class="btn"href="login.php">Login </a> If already registered
- </form>
- <style>
- h1 {text-align:center}
- </style>
- </form>
- </div>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement