DEV Community

Elijah Emmanuel
Elijah Emmanuel

Posted on

the reg

FORM

<?php
include_once('header.php');
include('../validate/register.php');
?>




<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./styles/nubastyle.css">
    <link rel="stylesheet" href="../styles/flick/flickity.css">
    <script src="../styles/flick/flickity.pkgd.min.js"></script>
    <script src="../styles/jquery/jquery.min.js"></script>
    <link rel="stylesheet" href="../public/styles/style.css">
    <title>LIBRARY SIGNUP</title>
</head>
<style>

</style>

<body>

    <br>
    <div class="container ml-lg-5 mr-lg-5">


        <div class="row  ml-lg-5 mr-lg-5 p-lg-5">
            <div class="col  ml-lg-5 mr-lg-5 p-lg-5 shadow">
                <form action="" method="POST">

                    <label for="username"><i class="fa fa-user"></i>User Name</label>
                    <input type="text" name="username" class="form-control">
                    <div class="text-danger"><?php echo $username_err?></div><br>


                    <label for="regno">Reg No</label>
                    <input type="text" name="regno" class="form-control">
                    <div class="text-danger"><?php echo $regno_err?></div><br>

                    <label for="email"><i class="fas fa-envelope"></i>Email </label>
                    <input type="email" name="email" class="form-control" required>

                    <label for="department"> <i class="fa fa-house-user"></i> Department</label>
                    <input type="text" name="department" class="form-control">

                    <label for="password"><i class="fa fa-key"></i> Password</label>
                    <input type="password" name="password" class="form-control" required>

                    <input type="submit" value="SUBMIT" class="btn btn-outline-primary">
                </form>
            </div>
        </div>
    </div>


    <?php include_once('../public/bottom.php')?>

</body>


</html>
Enter fullscreen mode Exit fullscreen mode

PHP CODE

<?php 
$regno_err=$username_err="";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if (preg_match('/^[a-zA-Z0-9]+$/', $_POST['username']) == 0) {
    $username_err='Username is not valid!';
}
    if(empty(trim($_POST['regno']))){
        $regno_err="please fill in this field";
    }elseif($stmt = $conn->prepare('SELECT id, username FROM user WHERE regno = ?')) {
        // Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
        $stmt->bind_param('s', $_POST['regno']);
        $stmt->execute();
        $stmt->store_result();
        // Store the result so we can check if the account exists in the database.
        if ($stmt->num_rows > 0) {
            // Username already exists
            $regno_err= 'regno exists, please choose another!';



        } else {
            $sql="INSERT INTO user(username,regno,email,password,department) VALUES(?,?,?,?,?)";
            if($stmt=$conn->prepare($sql)){
                $password=password_hash($password,PASSWORD_DEFAULT);
                $username=mysqli_real_escape_string($conn,$_POST['username']);
                $regno=mysqli_real_escape_string($conn,$_POST['regno']);
                $email=mysqli_real_escape_string($conn,$_POST['email']);
                $department=mysqli_real_escape_string($conn,$_POST['department']);

                $stmt->bind_param('sssss',$username,$regno,$email,$password,$department);
                if($stmt->execute()){
                    ECHO '<script>alert("SUCCESSFULLY REGISTERED")</script>';
                }
            }else{
                echo "something went wrong";
            }
        }

    } else {
        // Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
        echo 'Could not prepare statement!';
    }
}$conn->close();
?>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)