Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form method "POST" id="regform" action="register.php">
- <input type="text" username="username" placeholder="Please enter your
- email"/>
- <input type="text" password="password" placeholder="Please create a
- password"/>
- <input type="submit" value="enter"/>
- </form>
- <?php
- $servername = "localhost";
- $dbusername = "root";
- $dbpassword = "root";
- $dbname = "fyp";
- try{
- $pdostmt = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,
- $dbpassword);
- $pdostmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- echo "connected succesfully";
- }
- catch(PDOException $e)
- {
- print "Error! Unable to connect: " . $e->getMessage() . "<br/>";
- die();
- }
- ?>
- <?php
- // Starting the session and connecting to the DB
- session_start();
- require_once'connect.php';
- if(isset($_POST['register'])){
- $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
- $pass = !empty($_POST['password']) ? trim($_POST['password']) : null;
- $sql = "SELECT COUNT(username) AS num FROM users WHERE username = :username";
- $stmt = $pdo->prepare($sql);
- $stmt->bindValue(':username', $username);
- $stmt->execute();
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- if($row['num'] > 0){
- die('That username already exists!');
- }
- $sql = "INSERT INTO users (username, password) VALUES (:username,
- :password)";
- $stmt = $pdo->prepare($sql);
- $stmt->bindValue(':username', $username);
- $stmt->bindValue(':password', $passwordHash);
- $result = $stmt->execute();
- if($result){
- echo 'Thank you for registering with our website.';
- header("Location: index.php");
- }
- else {
- echo '<b> There has been an error please contact support </b>';
- }
- }
- ?>
Add Comment
Please, Sign In to add comment