Conncet to database



CRUD is an acronym for Create, Read, Update, and Delete. As the name suggests, these operations manipulate data in a database that is important to any web application's basic functionality. We can create a PHP application coupled with a MySQL database to perform these operations.

Connection.php

<?php
$server = 'localhost';
$host = 'root';
$password ="";
$dbname = "blog";
 
$conn = mysqli_connect($server ,$host,$password,$dbname);
if(!$conn)
{
    echo "connection not connect";
}
?>

Insert.php

<?php
include 'connection.php';
if(isset($_POST['submit']))
{
    $name=$_POST['name'];
    $email=$_POST['email'];
    $dob=$_POST['dob'];

$sql="INSERT INTO tb_blog(`name`,`email`,`dob`)
VALUES('$name','$email','$dob')";
 $query=mysqli_query($conn,$sql);
if($query){
    echo "Record Insert";
}
else{
    echo "Record not Insert";
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" >
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-sm-2"></div>
            <div class="col-sm-6">
    <form action="#" method="POST">
        <h1 class="text-center">Registration Form</h1>
        <label>Name</label>
        <input type="text" name="name" class="form-control">
        <label>Email</label>
        <input type="text" name="email" class="form-control">
        <label>Dob</label>
        <input type="Date" name="dob" class="form-control"><br>
      <button type="submit" class="btn btn-success" name="submit">submit</button>
    </form>

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

</body>
</html>

Displya.php

<?php
include 'connection.php';
$sql="SELECT * FROM tb_blog";
$query=mysqli_query($conn,$sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" >
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-sm-2"></div>
            <div class="col-sm-6">
            <table class="table">
  <thead>
    <tr>
      <th scope="col">Id</th>
      <th scope="col">Name</th>
      <th scope="col">Email</th>
      <th scope="col">Dob</th>
    </tr>
  </thead>
  <tbody>
    <?php
    while($row=mysqli_fetch_assoc($query)){
        ?>
        <tr>
        <th scope="row">  <?php echo $row['id']  ?></th>
        <td> <?php echo $row['name']  ?></td>
        <td> <?php echo $row['email']  ?></td>
        <td> <?php echo $row['dob']  ?></td>
      </tr>

   
   <?php    
    }
    ?>
   
   
  </tbody>
</table>
<a href="insert.php" class="btn btn-primary" >Insert</a>
            </div>
        </div>
    </div>
</body>
</html>