Connecting a database to a web application is a fundamental process for creating dynamic and interactive websites. Databases store and manage the data your application relies on, ensuring smooth user experiences and efficient functionality. This guide provides a detailed, step-by-step explanation of how to connect a database to your web application.

Step 1: Understand Your Project Requirements.

  • install xamp server.
  • chose a text editor like a notepad, or sublime text editor.
  • now to xamp folder and open htdos folder
  • make website folder.
  • inside web site forder make index.php file 
  • inside web site folder make css folder, and inside the css folder make index.css file

Code for project

 
<!DOCTYPE html>
<html>
<head>
    <meta charset=”utf-8″>
    <meta name=”viewport” content=”width=device-width, initial-scale=1″>
    <title>Login</title>
    <link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css”>
    <link rel=”stylesheet” type=”text/css” href=”css/index.css”>
</head>
<body>
<div class=”form-container”>
    <div class=”title”>Welcome Back</div>
    <form action=”#” method=”POST” autocomplete=”off”>
        <div class=”input_field”>
            <label for=”useremail”>Email address</label>
            <i class=”fas fa-envelope”></i>
            <input type=”email” id=”useremail” name=”useremail” placeholder=”Email address” required>
        </div>
        <div class=”input_field”>
            <label for=”userpass”>Password</label>
            <i class=”fas fa-lock”></i>
            <input type=”password” id=”userpass” name=”userpass” placeholder=”Password” required>
        </div>
        <div class=”options”>
            <label class=”remember-me”>
                <input type=”checkbox” name=”remember”> Remember me
            </label>
            <a href=”#” class=”forgot-password”>Forgot password?</a>
        </div>
        <button type=”submit” class=”btn” id=”login” name=”login”>Sign In</button>
        <div class=”divider”>
            <span>or continue with</span>
        </div>
        <div class=”social-buttons”>
            <button type=”button” class=”social-btn google”>
                <i class=”fab fa-google”></i>
            </button>
            <button type=”button” class=”social-btn facebook”>
                <i class=”fab fa-facebook-f”></i>
            </button>
            <button type=”button” class=”social-btn apple”>
                <i class=”fab fa-apple”></i>
            </button>
        </div>
 
    </form>
 
    <div class=”footer-links”>
        <p>Don’t have an account? <a href=”register.php”>Sign up here</a></p>
    </div>
 
</div>
</body>
</html>
<?php
session_start();
include(‘connection.php’);
//error_reporting(0);
if (isset($_POST[‘login’])) {
// code…
$useremail = $_POST[‘useremail’];
$userpass = $_POST[‘userpass’];
$query = “SELECT *FROM users WHERE email= ‘$useremail’ AND password =’$userpass'”;
$data = mysqli_query($conn, $query);
if (mysqli_num_rows($data)==1) {
$_SESSION[‘useremail’] = $useremail;
header(‘location:dashboard.html’);
exit();
 
}
else{
echo “<p style=’color:red; text-align:center;’> enter the valid email and password</p>”;
}
}
?>

/* Reset and base styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: “Poppins”, sans-serif;
}

body {
background: linear-gradient(135deg, #6a11cb, #2575fc);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}

/* Glassmorphic form container */
.form-container {
background: rgba(255, 255, 255, 0.15);
border-radius: 20px;
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.18);
width: 400px;
padding: 40px 30px;
color: #fff;
animation: fadeIn 1s ease-in-out;
}

/* Title */
.title {
text-align: center;
font-size: 28px;
font-weight: 600;
margin-bottom: 25px;
text-transform: capitalize;
}

/* Input fields */
form div {
position: relative;
margin-bottom: 20px;
}

label {
display: block;
margin-bottom: 8px;
font-size: 14px;
color: #e0e0e0;
}

input[type=”email”],
input[type=”password”] {
width: 100%;
padding: 12px 40px 12px 15px;
border-radius: 10px;
border: none;
outline: none;
background: rgba(255, 255, 255, 0.2);
color: #fff;
font-size: 15px;
}

input::placeholder {
color: #ddd;
}

.fas {
position: absolute;
right: 15px;
top: 38px;
color: #fff;
}

/* Options */
.options {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
font-size: 14px;
}

.options a {
color: #f1f1f1;
text-decoration: none;
transition: 0.3s;
}

.options a:hover {
color: #ffd369;
}

/* Button */
.btn {
width: 100%;
background: #00c6ff;
background: linear-gradient(45deg, #00c6ff, #0072ff);
border: none;
color: white;
padding: 12px;
font-size: 16px;
font-weight: 600;
border-radius: 10px;
cursor: pointer;
transition: 0.3s;
}

.btn:hover {
transform: translateY(-3px);
background: linear-gradient(45deg, #0072ff, #00c6ff);
}

/* Divider */
.divider {
text-align: center;
margin: 20px 0;
position: relative;
color: #ccc;
font-size: 14px;
}

.divider span {
background: rgba(0, 0, 0, 0.3);
padding: 0 10px;
border-radius: 5px;
}

/* Social buttons */
.social-buttons {
display: flex;
justify-content: center;
gap: 15px;
}

.social-btn {
width: 45px;
height: 45px;
border: none;
border-radius: 50%;
color: white;
font-size: 18px;
cursor: pointer;
transition: 0.3s;
}

.social-btn.google {
background: #db4437;
}

.social-btn.facebook {
background: #3b5998;
}

.social-btn.apple {
background: #000;
}

.social-btn:hover {
transform: scale(1.1);
}

/* Footer link */
.footer-links {
text-align: center;
margin-top: 20px;
font-size: 14px;
}

.footer-links a {
color: #00c6ff;
text-decoration: none;
font-weight: 500;
}

.footer-links a:hover {
text-decoration: underline;
}

/* Animation */
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}

/* Responsive design */
@media (max-width: 450px) {
.form-container {
width: 90%;
padding: 30px 20px;
}
.title {
font-size: 24px;
}
}

 
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>User registaion</title>
<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css”>
<link rel=”stylesheet” type=”text/css” href=”css/register.css”>
</head>
<body>
<div class=”container”>
<div class=”title”> Create Account</div>
 
<form action=”#” method=”post” autocomplete=”off”>
<div class=”input_field”>
<label>First name</label>
<i class=”fas fa-user”></i>
<input type=”text” name=”fname” required placeholder=”Enter first name”>
</div>
<div class=”input_field”>
<label>Last name</label>
<i class=”fas fa-user”></i>
<input type=”text” name=”lname” required placeholder=”Enter last name”>
</div>
<div class=”input_field”>
<label>Email</label>
<i class=”fas fa-envelope”></i>
<input type=”email” name=”email” required placeholder=”Enter email”>
</div>
<div class=”input_field”>
<label>Password</label>
<i class=”fas fa-lock”></i>
<input type=”password” name=”password” required placeholder=”Enter your password”>
</div>
<div class=”input_field”>
<label>Conform Passsword</label>
<i class=”fas fa-lock”></i>
<input type=”password” name=”cpassword” required placeholder=”Conform your password:”>
</div>
<div class=”input_field”>
<label>Date of Birth</label>
<i class=”fas fa-calender”></i>
<input type=”Date” name=”dob” >
</div>
<div class=”input_field”>
<label>Gender</label>
<i class=”fa fa-venus-mars”></i>
<select name=”gender” required>
<option value=””>Select Gender</option>
<option value=”male”>Male</option>
<option value=”female”>Female</option>
<option value=”other”>Other</option>
</select>
</div>
<div class=”input_field”>
<label>Phone</label>
<i class=”fas fa-phone”></i>
<input type=”numbers” name=”phone” required placeholder=”Enter phone number”>
</div>
<div class=”input_field”>
<label>Address</label>
<i class=”fas fa-home”></i>
<input type=”text” name=”address” required placeholder=”Enter address”>
</div class  input_field>
 
<div class=”input_field”>
<input type=”submit” name=”register” value=”Regiater” class=”btn”>
</div>
<div class=”input_field”>
<div class=”p”>Already have an account?<a href=”index.php”>Login here</a></div>
</div>
 
</div>
</form>
 
</body>
</html>
<?php
include(“connection.php”);
if(isset($_POST[‘register’]))
{
$fname    = $_POST[‘fname’];
$lname    = $_POST[‘lname’];
$email    = $_POST[’email’];
$password = $_POST[‘password’];
$cpwd     = $_POST[‘cpassword’];
$dob      = $_POST[‘dob’];
$gender   = $_POST[‘gender’];
$phone    = $_POST[‘phone’];
$address  = $_POST[‘address’];
    $query    = “INSERT INTO USERS VAlUES(‘$fname’, ‘$lname’,’$email’,’$password’,’$cpwd’,’$dob’,’$gender’,’$phone’,’$address’)”;
    $data = mysqli_query($conn, $query); 
 
if ($data) {
echo “Data inserted in databa correctely”;
}
else{
echo “Data not inserted into data base”.mysql_error();
}
}
 
?>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: ‘Arial’, sans-serif;
}
body{
background: linear-gradient(135deg, #667eea 0%, #764ba2 100% );
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
 
}
.container {
            background: white;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            width: 100%;
            max-width: 700px;
        }
        
        .title {
            text-align: center;
            font-size: 28px;
            font-weight: bold;
            margin-bottom: 30px;
            color: #333;
        }
.input_field {
            position: relative;
            margin-bottom: 20px;
        }
        .input_field label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
            font-size: 20px;
        } 
  .input_field i {
            position: absolute;
            top: 65%;
            left: 12px;
            transform: translateY(-50%);
            color: #F54927;
        }
     .input_field input, 
        .input_field select {
            width: 100%;
            padding: 12px 12px 12px 40px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease;
        }
        
        .input_field input:focus, 
        .input_field select:focus {
            outline: none;
            border-color: #667eea;
        }
  .btn {
            width: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 15px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 18px;
            font-weight: bold;
            transition: transform 0.2s ease;
            margin-top: 10px;
        }
        
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        } 
  .input_field p {
            text-align: center;
            margin-top: 20px;
        }
        
        .input_field a {
            color: #667eea;
            text-decoration: none;
            font-weight: bold;
        }
        
        .input_field a:hover {
            text-decoration: underline;
        }
          
                                       
<?php
$servername =”localhost”;
$username =”root”;
$password =””;
$dbname =”website”;
$conn = mysqli_connect($servername, $username, $password, $dbname);
if ($conn) {
//echo “Connection ok”;
}
else{
echo “Connection failed” .mysqli_connect_error();
}
?>

Code for project

<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>Dasboadrd</title>
<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css”>
<link rel=”stylesheet” type=”text/css” href=”css/dashboard.css”>
</head>
<body>
<style>
.update{
    background-color: #00ffff;
    color: white;
    box-shadow: 0;
    border-radius: 5px;
    width: 100px;
    font-weight: 25px;
    cursor: pointer;
}
.delet{
    background-color: #8b0000;
    color: white;
    box-shadow: 0;
    border-radius: 5px;
    width: 100px;
    font-weight: 20px;
    cursor: pointer;
}
</style> 
 
 
<div class=”sidebar”>
<div profile>
<img src=”photo/man.png”>
<h3>Basant</h3>
<p style=”color:#3ecf8e;”>Online<p>
</div>
<ul>
        <li><i class=”fa fa-home”></i> Dashboard</li>
        <li><i class=”fa fa-layer-group”></i> Table</li>
        <li><i class=”fa fa-cubes”></i>Information</li>
        <li><i class=”fa fa-table”></i> About</li>
        <li><i class=”fa fa-mobile”></i> Log out</li>
    </ul>
</div>
<div class=”header”>
<h2>Dashboard</h2>
<div class=”user-area”>
<img src=”photo/man.png” style=”width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;”>Basant
</div>
   </div>
   <?php
   include(“connection.php”);
   session_start();
   $query = “SELECT * FROM USERS”;
   $data = mysqli_query($conn, $query);
   ?>
    
    <main class=”content”>
    <div class=”table-container”>
    <table border=”2px” cellspacing=”7″ width=”100%”>
    <tr>
    <th>id</th>
    <th>first name</th>
    <th>last name </th>
    <th>Email</th>
    <th>Gender</th>
    <th>Phone</th>
    <th>Address</th>
    <th>Operation</th>
   
   
   
    </tr>
           <?php
while ($row = mysqli_fetch_assoc($data)) {
    echo “<tr>
            <td>{$row[‘id’]}</td>
            <td>{$row[‘fname’]}</td>
            <td>{$row[‘lname’]}</td>
            <td>{$row[’email’]}</td>
            <td>{$row[‘gender’]}</td>
            <td>{$row[‘phone’]}</td>
            <td>{$row[‘address’]}</td>
            <td>
                <a href=’update.php?id={$row[‘id’]}’>
                    <button class=’update’>Update</button>
                </a>
                <a href=’delet.php?id={$row[‘id’]}’ onclick=\”return confirm(‘Are you sure?’)\”>
                    <button class=’delet’>Delete</button>
                </a>
            </td>
          </tr>”;
}
?>
 
    </table>
    </div>
    </main>
 
</body>
</html>

body {
margin: 0;
font-family: Arial, sans-serif;
background: #f4f6f9;
}
/* Sidebar */
.sidebar {
width: 250px;
height: 100vh;
background: #0f1f38;
color: #fff;
position: fixed;
left: 0;
top: 0;
overflow-y: auto;
}
.sidebar .profile {
text-align: center;
padding: 20px 0;
border-bottom: 1px solid #223457;
}
.sidebar img {
width: 70px;
height: 70px;
border-radius: 50%;
border: 2px solid #fff;
}
.sidebar h3 {
margin: 10px 0 5px;
}
.sidebar ul {
list-style: none;
padding: 0;
}
.sidebar ul li {
padding: 14px 20px;
cursor: pointer;
border-bottom: 1px solid #1a2d4e;
}
.sidebar ul li i {
margin-right: 10px;
}
.sidebar ul li:hover {
background: #1a2d4e;
}
/* Header */
.header {
margin-left: 250px;
background: #0f1f38;
color: #fff;
padding: 15px 30px;
display: flex;
justify-content: space-between;
align-items: center;
}
/* Dashboard content */
.content {
margin-left: 250px;
padding: 30px;
}
.table-container {
margin-top: 25px;
background: white;
border-radius: 12px;
padding: 20px;
box-shadow: 0px 5px 18px rgba(0,0,0,0.1);
}

<?php
include(“connection.php”);

/* 1. Validate ID */
if (!isset($_GET[‘id’]) || !is_numeric($_GET[‘id’])) {
die(“Invalid user ID”);
}

$id = (int) $_GET[‘id’];

/* 2. Fetch existing user data */
$query = “SELECT * FROM users WHERE id = $id”;
$data = mysqli_query($conn, $query);

if (mysqli_num_rows($data) != 1) {
die(“User not found”);
}

$user = mysqli_fetch_assoc($data);

/* 3. Handle update */
if (isset($_POST[‘update’])) {

$fname = mysqli_real_escape_string($conn, $_POST[‘fname’]);
$lname = mysqli_real_escape_string($conn, $_POST[‘lname’]);
$email = mysqli_real_escape_string($conn, $_POST[’email’]);
$dob = mysqli_real_escape_string($conn, $_POST[‘dob’]);
$gender = mysqli_real_escape_string($conn, $_POST[‘gender’]);
$phone = mysqli_real_escape_string($conn, $_POST[‘phone’]);
$address = mysqli_real_escape_string($conn, $_POST[‘address’]);

$update = “UPDATE users SET
fname = ‘$fname’,
lname = ‘$lname’,
email = ‘$email’,
dob = ‘$dob’,
gender = ‘$gender’,
phone = ‘$phone’,
address = ‘$address’
WHERE id = $id”;

if (mysqli_query($conn, $update)) {
header(“Location: dashboard.php”);
exit;
} else {
$error = “Update failed: ” . mysqli_error($conn);
}
}
?>
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>Update User</title>

<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css”>
<link rel=”stylesheet” href=”css/register.css”>
</head>
<body>

<div class=”container”>
<div class=”title”>Update Account</div>

<?php if (isset($error)) echo “<p style=’color:red;’>$error</p>”; ?>

<form method=”post”>
<div class=”input_field”>
<label>First Name</label>
<input type=”text” name=”fname” value=”<?= htmlspecialchars($user[‘fname’]) ?>” required>
</div>

<div class=”input_field”>
<label>Last Name</label>
<input type=”text” name=”lname” value=”<?= htmlspecialchars($user[‘lname’]) ?>” required>
</div>

<div class=”input_field”>
<label>Email</label>
<input type=”email” name=”email” value=”<?= htmlspecialchars($user[’email’]) ?>” required>
</div>

<div class=”input_field”>
<label>Date of Birth</label>
<input type=”date” name=”dob” value=”<?= htmlspecialchars($user[‘dob’]) ?>”>
</div>

<div class=”input_field”>
<label>Gender</label>
<select name=”gender” required>
<option value=”male” <?= ($user[‘gender’]==”male”)?’selected’:” ?>>Male</option>
<option value=”female” <?= ($user[‘gender’]==”female”)?’selected’:” ?>>Female</option>
<option value=”other” <?= ($user[‘gender’]==”other”)?’selected’:” ?>>Other</option>
</select>
</div>

<div class=”input_field”>
<label>Phone</label>
<input type=”text” name=”phone” value=”<?= htmlspecialchars($user[‘phone’]) ?>” required>
</div>

<div class=”input_field”>
<label>Address</label>
<input type=”text” name=”address” value=”<?= htmlspecialchars($user[‘address’]) ?>” required>
</div>

<div class=”input_field”>
<input type=”submit” name=”update” value=”Update” class=”btn”>
</div>
</form>
</div>

</body>
</html>

<?php
include(“connection.php”);

if (!isset($_GET[‘id’]) || !is_numeric($_GET[‘id’])) {
die(“Invalid ID”);
}

$id = (int) $_GET[‘id’];

$query = “DELETE FROM users WHERE id = $id”;

if (mysqli_query($conn, $query)) {
header(“Location: dashboard.php”);
exit;
} else {
echo “Delete failed: ” . mysqli_error($conn);
}
?>

<?php
session_start();
session_unset();
session_destroy();
header(“Location: index.php”);
exit();
?>

error: Content is protected !!
Scroll to Top