Friday 28 April 2017

Php form with Sql connectivity

PHP Forms with MYSQL 


 In this section we are going to actually introduce you the PHP development 

In that we are going to make Html form and stores the date which is provided by the user in the Database.


Before that if you dont know how to setup server
(click here)

and finally some development

lets make some form in Html which will

1)
Now for designing a form write the given code in index.php file
and write some Html code as below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>
<title>Add Employee</title>
</head>
<body>
<form method="post" action="">
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>Department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />
<br />
<input type="submit" value="Add Employee">
</form>
</body>
</html>

As above written Html code please give every <INPUT> tag special  NAME 
cause it is going to help us to connect the our Html code to database via PHP  

Now make another file name as insert.php 
and write below mention code  

in this page in the 
firsr php block we are going to make connection with database
in that check the connection 
after that make sql vaiable and insert the query in that variable 
check the insertion process and if work is done close the connection

<?php
// make connection with database 
$connect=mysqli_connect('localhost','root','password','mydatabase');
 // check connection is working or not
if(mysqli_connect_errno($connect))
{
echo 'Failed to connect';
}
 
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//Execute the query
mysqli_query($connect"INSERT INTO employees1(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')");
// check data in insarted in the database or not 


if(mysqli_affected_rows($connect) > 0){
echo "<p>Employee Added</p>";
echo "<a href="index.html">Go Back</a>";
} else {
echo "Employee NOT Added<br />";
echo mysqli_error ($connect);
}
// echo the values which are inserted 

echo $_POST['first_name'];
echo '<br />';
echo $_POST['email'];
?>

and the sql query for table is 

CREATE TABLE IF NOT EXIST 'employees1'(
'first_name' varchar(20),
'last_name' varchar(20),
'department' varchar(10),
'email' varchar(20),
PRIMARY KEY ('first_name')
)engin=Innodb;


 wait for the Next post till then check previous ones
( Click here for previous Posts about Php )


Contact use for business projects or  college projects
Facebook Gmail  Google +

0 comments:

Post a Comment

Thank you