Use of database in PHP

Introduction of Database 

A database is  a collection of program and tools that facilitate users to create and maintain the database. It provides the enterprise  with centralized control of its operational data- which is one of its most relevant assets.

We will be using MYSQL it is the most popular database system in the world. It stands for structure query language. The first word MY stands for the daughter's name of co-founder Michael Widenius. It is an open source, most popular and it helps to execute the command or query.

Example of  Students table

The table is divided in three parts  Column     Rows    and    Field

 
IdNameAddressclass
1Karna SinghL 780 the Lotus arena sector70, Noida 8th
2Diva verma348 , Brigton Towers, Lokhandwala, Andheri (west)10th
3Sikha yadav87, Raj Apartments, JyotiGunj Noida - 49817612th
4Vipin rana54, Mukti Chowk, Delhi- 1605986th

 

Creating a database in  PHPmyadmin       

PHPmyadmin allows us to create a database we can create database and manage the database and many more things .

Open your browser on address bar write localhost/phpmyadmin  click Enter.

click on database option and create a database gives a name to your database School

Or click on SQL write query to make your database 

CREATE DATABASE School

There are many more options in the database such as SQL, status, users, export, import, settings, replications, variables and more each has different functions and different use .               

 

Creating Tables and Inserting Data in database       

Click on the database School and create a table students and  the number is 4 

 After making table, click save there you get the table students and then enter the data in the students table.

In this table, we use id as an integer and A_I auto increment so it increases as you enter any record.

We can edit and drop our students table .  

 

Design student view page

By using bootstrap classes

student.php

<html>

<head>
    <title>This is demo</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <h2>Student Form</h2>
        <form class="form-horizontal" action="student.php" method="post">
            <div class="form-group">
                <label class="control-label col-sm-2">Name:</label>
                <div class="col-sm-7">
                    <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2" for="pwd">Address:</label>
                <div class="col-sm-7">
                    <input type="text" class="form-control" id="address" placeholder="Enter Address" name="address">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2">Class:</label>
                <div class="col-sm-7">
                    <input type="text" class="form-control" id="class" placeholder="Enter Class" name="class">
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" name="submit" class="btn btn-success">Submit</button>
                </div>
            </div>
        </form>
    </div>

</body>

</html>

Receiving Post data from check

student.php

<?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $address = $_POST['address'];
    $class = $_POST['class'];
    echo  $name;
    echo  $address;
    echo  $class;
}
?>

<html>

<head>
    <title>This is demo</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <h2>Student Form</h2>
        <form class="form-horizontal" action="student.php" method="post">
            <div class="form-group">
                <label class="control-label col-sm-2">Name:</label>
                <div class="col-sm-7">
                    <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2">Address:</label>
                <div class="col-sm-7">
                    <input type="text" class="form-control" id="address" placeholder="Enter Address" name="address">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2">Class:</label>
                <div class="col-sm-7">
                    <input type="text" class="form-control" id="class" placeholder="Enter Class" name="class">
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" name="submit" class="btn btn-success">Submit</button>
                </div>
            </div>
        </form>
    </div>

</body>

</html>


                   

 

To check the data in another way 

Whether the data is received in the form or not we have to check the data by using if and else statement in PHP.

student.php

//Using if and else condition to check the form data
<?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $address = $_POST['address'];
    $class = $_POST['class'];
    If ($name && $address && $class){
        echo  $name;
        echo  $address;
        echo  $class;
    } else {
        echo "Please fill the form";
    }
}
?>

 

Connection of database by using PHP   

To make a connection of database we use the mysqli_connect. mysqli_connect takes 4 arguments and return connection link:

1. Host
2. Database User
3. Database Password
4. Database Name

 

student.php

<?php
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $address = $_POST['address'];
    $class = $_POST['class'];
// code for database connection 
   $connection = mysqli_connect('localhost', 'root', '', 'school');

If ($connection) {
    echo "Your connection has been established";
} else {
    echo "Database Error No.: " . mysqli_connect_errno() . PHP_EOL;
    echo "Database Error: " . mysqli_connect_error() . PHP_EOL;
    die("Please try again");
}

}
?>

                                                                     For more use click here              

Keywords: