Friday, 26 February 2021

Storing Data into a Database

STEPS FOR STORING DATA INTO DATABASE

Step:1 Create table (Eg. student) in database(eg:test)

Step:2 Create client side webpage(insert.html) for getting data from users and store into D:\xampp\htdocs folder.

<!DOCTYPE html>
<html>
<head>
<title>Store Data into Database</title>
<style>
h1,div
{
text-align:center;
}
#sds-header
{
color:white;
height:50px;
width:100%;
background-color:green;
font-size:25px;
padding-top:15px;
}
#sds-form
{
color:blue;
height:550px;
width:100%;
background-color:yellow;
font-size:25px;
padding-top:25px;
}
</style>
</head>
<body>
<div id="sds-header">
Student Database System
</div>
<div id="sds-form">
<form name="myform" action="insertdb.php" method="get">
<input type="text" name="uroll" placeholder="Roll Number"><br><br>
<input type="text" name="uname" placeholder="Name"><br><br>
<input type="text" name="uage" placeholder="Age"><br><br>
<input type="text" name="udept" placeholder="Department"><br><br>
<input type="submit" value="Insert">
</form>
</div>
</body>
</html>

Step:3 Create an another webpage(insertdb.php) in D:\xampp\htdocs folder using PHP for collecting data from previous page and storing those data into a table(eg. student) present in the database(eg. test).

<?php
$rollno=$_GET['uroll'];
$name=$_GET['uname'];
$age=$_GET['uage'];
$dept=$_GET['udept'];

// Create connection
$conn = new mysqli("localhost", "siva", "123", "test");
// Check connection
if ($conn->connect_error) 
{
die("Connection failed:" . $conn->connect_error);
}

$sql = "INSERT INTO STUDENT VALUES('$rollno','$name','$age','$dept')";

if(mysqli_query($conn, $sql))
{
echo "Records inserted successfully.";
else
{
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
$conn->close();
?>

Step:4 Run the first page http://localhost/insert.html




Step:5 Enter details in the form and click insert

Step:6 Now you  will see the following screen after successful insertion.

THANK YOU