How Registration forms works using PHP

0
124

register.php

<?php
include('security.php');
include('includes/header.php');
include('includes/navbar.php');


?>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">

<div class="modal fade" id="addadminprofile" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Add Admin Data</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="code.php" method="POST">

        <div class="modal-body">

            <div class="form-group">
                <label> Username </label>
                <input type="text" name="username" class="form-control" placeholder="Enter Username">
            </div>
            <div class="form-group">
                <label>Email</label>
                <input type="email" name="email" class="form-control checking_email" placeholder="Enter Email">
                <small class="error_email" style="color: red;"></small>
            </div>
            <div class="form-group">
                <label>Password</label>
                <input type="password" name="password" class="form-control" placeholder="Enter Password">
            </div>
            <div class="form-group">
                <label>Confirm Password</label>
                <input type="password" name="confirmpassword" class="form-control" placeholder="Confirm Password">
            </div>

            <input type="hidden" name="usertype" value="admin">

        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="submit" name="registerbtn" class="btn btn-primary">Save</button>
        </div>
      </form>

    </div>
  </div>
</div>

<div class="container fluid">
    <div class="card shadow mb-4">
        <div class="card header py-3">
            <h6>Admin Profile

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addadminprofile">
       Add Admin Profile 
</button>
</h6>
</div>

<div class="card-body">

<?php
if(isset($_SESSION['success']) && $_SESSION['success'] !='') 
{
    echo '<h2> '.$_SESSION['success'].' <h2>';
    unset($_SESSION['success']);

}

if(isset($_SESSION['status']) && $_SESSION['status'] !='') 
{
    echo '<h2> '.$_SESSION['status'].' <h2>';
    unset($_SESSION['status']);

}

if(isset($_SESSION['status2']) && $_SESSION['status2'] !='') 
{
    echo '<h2> '.$_SESSION['status2'].' <h2>';
    unset($_SESSION['status2']);

}

?>
    <div class="table-responsive">
    <?php

$connection = mysqli_connect("localhost","root","","lottery");
                $query = "SELECT * FROM register";
                $query_run = mysqli_query($connection, $query);
            ?>
        <table class="table table-bordered" id="datatable" width="100" cellspacing="0">
            <thead>
                <tr>
                <th> ID </th>
                <th> Username </th>
                <th> Email </th>
                <th> Password </th>
                <th> user type </th>
                <th> Edit </th>
                <th> Delete </th>
</tr>
</thead>
<tbody>

<?php
if(mysqli_num_rows($query_run) > 0)
{
    while($row = mysqli_fetch_assoc($query_run))
    {
?>
        <tr>
        <td><?php echo $row['id'];?></td>
        <td><?php echo $row['username'];?></td>
        <td><?php echo $row['email'];?></td>
        <td><?php echo $row['password'];?></td>
        <td><?php echo $row['usertype'];?></td>
        <td>
            <form action="registeredit.php" method="post">
                <input type="hidden" name="edit_id" value="<?php echo $row['id'];?>">
            <button type="submit" name="edit_btn" class="btn btn-success"> EDIT </button>
            </form>
        </td>
        <td>
        <form action="code.php" method="post">
        <input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">
        <button type="submit"name="delete_btn" class="btn btn-danger"> DELETE </button>
        </form>
    </td>
</tr>
<?php
}

}
else
{
    echo "No Record Found";
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php include('includes/scripts.php');
?>

code.php

<?php
include('security.php');
$connection = mysqli_connect("localhost","root","","lottery");

if(isset($_POST['registerbtn']))
{
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['confirmpassword'];
    $usertype =$_POST['usertype'];

    $email_query = "SELECT * FROM register WHERE email='$email' ";
    $email_query_run = mysqli_query($connection, $email_query);
    if(mysqli_num_rows($email_query_run) > 0)
    {
        $_SESSION['status'] = "Email Already Taken. Please Try Another one.";
        $_SESSION['status_code'] = "error";
        header('Location: register.php');  
    }
    else
    {
        if($password === $cpassword)
        {
            $query = "INSERT INTO register (username,email,password,confirmpassword,usertype) VALUES ('$username','$email','$password','$cpassword','$usertype')";
            $query_run = mysqli_query($connection, $query);
            
            if($query_run)
            {
                //echo "Saved";
                $_SESSION['success'] = "Admin Profile Added";
                $_SESSION['status_code'] = "success";
                header('Location: register.php');
            }
            else 
            {
                $_SESSION['status'] = "Admin Profile Not Added";
                $_SESSION['status_code'] = "error";
                header('Location: register.php');  
            }
        }
        else 
        {
            $_SESSION['status2'] = "Password and Confirm Password Does Not Match";
            $_SESSION['status_code'] = "warning";
            header('Location: register.php');  
        }
    }

}
?>

LEAVE A REPLY

Please enter your comment!
Please enter your name here