How to Insert and Display Data into DB by using PHP and MYSQL

Welcome to MKinventions



hai friends!
In this Artical i am going to show you how to insert Data into Data Base by using PHP and MYSQL.
creating a new data dase and inserting data is very simple .
now we are going to create new data base by using the following steps.
here iam going to insert and display contact data into data base.


Step 1: contact.php                 Download
Step 2: display-data.php        Download
Step 3: db_connection.php    Download
Step 4: contact_db.php           Download
Step 5: Create Table in Data Base(SQL file) Download


Step 1: In this step we are going to create a new form and save it as contact.php (you can save it by your intrest).

<html> <head><title>MKinventions</title> <style> .form-control{background-color: aqua; width: 800px;height: 500px;border: 1px solid;border-radius: 10px; box-shadow: 0px 0px 105px 29px grey} .input { margin-top: 18px; height: 40px; width: 509px; border: 1px solid; border-radius: 10px; } .button{background-color: green; width: 100px;height: 40px; color: white; border: 1px solid;border-radius: 10px;} </style> </head> <body> <br /><br /><br /> <center> <div class="form-control"> <h1> MKinventions</h1> <h2> Enter Data</h2> <br /> <form action="contact_db.php" method="post"> <lable>Name</lable> <input class="input" name="name" placeholder="Enter Name" type="text" /><br /> <lable>Email</lable> <input class="input" name="email" placeholder="Enter Email" type="text" /><br /> <lable>Address</lable> <input class="input" name="address" placeholder="Enter Address" type="text" /><br /> <lable>Message</lable> <textarea class="input" name="message" placeholder="Enter Message"></textarea><br /> <input class="button" type="submit" value="Submit" /> </form> </div> </center> </body> </html>


step 2:Display your data in display-data.php page.
<html> <head><title>MKinventions</title> <style> .form-control{background-color: aqua; width: 800px;height: 500px;border: 1px solid;border-radius: 10px; box-shadow: 0px 0px 105px 29px grey} .table{width: 747px;border: 1px solid;} </style> </head> <body> <br><br><br> <center> <div class="form-control"> <h1>MKinventions</h1> <h2>Display Data</h2><br> <table cellspacing=10 cellpadding=5 class="table"> <thead><tr> <th>Sl.no</th> <th>Name</th> <th>Email</th> <th>Address</th> <th>Message</th> <th>Date/Time</th> </tr></thead> <tbody> <!--php $i=1; extract($_REQUEST); include("db_connection.php"); $sql=$conn--->query("SELECT * FROM `contact_table`"); while($record=$sql->fetch_assoc()) { ?> <tr> <th><!--php echo $record['id'];?--></th> <th><!--php echo $record['name'];?--></th> <th><!--php echo $record['email'];?--></th> <th><!--php echo $record['address'];?--></th> <th><!--php echo $record['message'];?--></th> <th><!--php echo $record['CreatedOn'];?--></th> </tr> <!--php $i++; } ?--> </tbody> </table> </div> </center> </body> </html>


step 3:we required this code to connect with data base db_connection.php
<!--php date_default_timezone_set('Asia/Kolkata'); $server="localhost"; $username="root"; $password=""; $dbname="apple"; $conn=new mysqli($server,$username,$password,$dbname); if($conn--->connect_error) { die("connection failed:".$conn->connection_error); } ?>


step 4:we need this code to insert data into Data Base ,all the contact form functionality works in this page ,this will helps to insert data into DB
<!--php extract($_REQUEST); include('db_connection.php'); $sql=$conn--->query("INSERT INTO `contact_table`(`name`, `email`, `address`, `message`, `CreatedOn`) VALUES('$name','$email','$address','$message',now())"); if($sql===TRUE) { echo"<script>window.location.assign('contact.php?success=success')</script>"; } else { echo"<script>window.location.assign('contact.php?success=failed')</script>"; echo $conn->error; } ?>


step 5: Create Table in Data Base Table name as (contact_table) in apple DB
CREATE TABLE `contact_table` ( `id` int(20) NOT NULL, `name` varchar(20) NOT NULL, `email` varchar(50) NOT NULL, `address` varchar(50) NOT NULL, `message` varchar(200) NOT NULL, `CreatedOn` datetime NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Post a Comment

0 Comments