Anda di halaman 1dari 8

Insert Data in Database Using PHP

Php is a server side scripting language it allows to store data into database at
server level. In this post I will illustrate you how insert data with Sql insert
query in PHP.
insert into tablename(column1,column2,...) values(value1,'value2',...);

Note : Value1 is written without single quotes, because it is


an integer value and value2 is a string.
For example : value1 = 1 and value2 = Albert.

First of all, I have created HTML form, in which method is set to post ,to hide
values in URL which flows on form submission.
Next, I wrote PHP code where the main functionality of insertion of values into
database is performed.

At MySql database I have created a database named colleges and table


named students which consists of five fields viz. student_id,
student_name, student_email, student_contact, student_address.

Here in the download code provided below there is file naming db.sql in
which sql queries are written for creating database, creating table and different
columns in it.
You just need to import it in your phpmyadmin it will create the required tables.
MY-SQL Code
CREATE DATABASE IF NOT EXISTS colleges;
CREATE TABLE students(
student_name varchar(255) NOT NULL,
student_email varchar(255) NOT NULL,
student_contact varchar(255) NOT NULL,
student_address varchar(255) NOT NULL
)

After that use relevant path to run the program, like for example :
localhost/insert.php

Now fill the form fields and submit it, data will be saved to MySql database and
a notification will be delivered Data Submitted successfully.

Watch out the live demo or download the code to use it

HTML File insert.php

Consists of form div form.

<!DOCTYPE html>
<html>
<head>
<title>PHP insertion</title>
<link href="css/insert.css" rel="stylesheet">
</head>
<body>
<div class="maindiv">

<!--HTML Form -->


<div class="form_div">
<div class="title">
<h2>Insert Data In Database Using PHP.</h2>
</div>
<form action="insert.php" method="post">
<!-- Method can be set as POST for hiding values in URL-->
<h2>Form</h2>
<label>Name:</label>
<input class="input" name="name" type="text" value="">
<label>Email:</label>
<input class="input" name="email" type="text" value="">
<label>Contact:</label>
<input class="input" name="contact" type="text" value="">
<label>Address:</label>
<textarea cols="25" name="address" rows="5"></textarea><br>
<input class="submit" name="submit" type="submit" value="Insert">
</form>
</div>
</div>
</body>
</html>

PHP Code segment

Insert form information into database.

<?php
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("colleges", $connection); // Selecting Database from Server
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['contact'];
$address = $_POST['address'];
if($name !=''||$email !=''){
//Insert Query of SQL
$query = mysql_query("insert into students(student_name, student_email,
student_contact, student_address) values ('$name', '$email', '$contact', '$address')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
mysql_close($connection); // Closing Connection with Server
?>

Css File insert.css

Includes basic styling of HTML elements.

@import "http://fonts.googleapis.com/css?family=Droid+Serif";
/* The Above Line Is To Import Google Font Style */

.maindiv {
margin:30px auto;
width:980px;
height:500px;
background:#fff;
padding-top:20px;
font-family:'Droid Serif',serif;
font-size:14px
}
.title {
width:500px;
height:70px;
text-shadow:2px 2px 2px #cfcfcf;
font-size:16px;
text-align:center
}
.form_div {
width:70%;
float:left
}
form {
width:300px;
border:1px dashed #aaa;
padding:10px 30px 40px;
margin-left:70px;
background-color:#f0f8ff
}
form h2 {

text-align:center;
text-shadow:2px 2px 2px #cfcfcf
}
textarea {
width:100%;
height:60px;
border-radius:1px;
box-shadow:0 0 1px 2px #123456;
margin-top:10px;
padding:7px;
border:none
}
.input {
width:100%;
height:30px;
border-radius:2px;
box-shadow:0 0 1px 2px #123456;
margin-top:10px;
padding:7px;
border:none;
margin-bottom:20px
}
.submit {
color:#fff;
border-radius:3px;
background:#1F8DD6;
padding:5px;
margin-top:40px;

border:none;
width:100%;
height:30px;
box-shadow:0 0 1px 2px #123456;
font-size:18px
}
p{
color:red;
text-align:center
}
span {
text-align:center;
color:green
}

Anda mungkin juga menyukai