Php configuration and database connection to iis
Step1: Download IIS(internet information services)
Step2: First we need to do is search control panel on the start menu and open it.
Step3: Go to programs.
Step4: Under programs and features click on Turn Windows features on or off
Step5: First enable internet information service if not enabled.
Step6: Click on + symbol of internet information services the following options will be appeared under that enable world wide web services.
Step7: Click on + symbol of world wide web services the following options will be appeared under that enable Application development
features.
features.
Step8: Click on + symbol of Application development features the following options will be displayed under that enable CGI if it is not
enabled.
enabled.
Step9: Search for iis on start menu the following window will be opened.
Step10: Double click on Handler Mappings then at the right side of the window under actions select add module mapping.
Step11: When you click on add module mapping the following window will be opened.
In this under Request path enter *.php
Under Module section select FastCGImodule
Under Executable select the php extracted file i.e php.cgi give that path.
Under Name it is our wish we can give any name under it no problem.
After filling the above details you will get the following window then click on yes
Step12: In this case in the connections part right click on sites and then select add website.
Step13: When you click on add website the following window will be appeared.
In this case site name is our wish name.
Physical path is for where to write php code.
And write any host name. Then click on ok following window will be opened.
Step14: Right click on our site and select Manage website under that select browse then automatically following window will be displayed.
Then enter the your php file name the following window will be opened.
localhost/Phpinfo.php run this on browser following output will be produced.
Here Phpinfo.php is our program.
Database connection:
Step1: First we need to do is install sqlsrv drivers for your php version.
After downloading it, you will find the following in your downloads.
Step2: Double click on it, it shows the following window under that click on yes.
Step3: After click on yes it will show you the following window under that select the path where you want to extract your files. Then click
on ok.
on ok.
Step4: Add the sqlsrv files to the ext folder in phpconfig folder.
Copy above two files and insert them into php folder under ext.
Step5: Adding sqlsrv files in php.ini files open php.ini file and search for extension-dir and comment out.
And add those two nts file names in php.ini folder as shown below.
Step6: Run the Phpinfo.php file on browser you will get sqlsrv page will be added to the info.
Step7: write a database connecting program in default folder.
When you click on your website under that select explore and write the php database connecting program.
db.php program:
Step8: Run the above program in browser the following output will be displayed.
Insertion of data into database through html form
Step1: For this we have to create a basic html form.
Code:
<html>
<head>
<style>
body{font-family:sans-serif;}
td {width:150px; padding:5px;}
.inp{width:250px; height:25px;}
#div1{width:850px; }
#heading{text-align:center;}
.inpname{text-align:right; font-size:14;}
.submit {text-align:right;}
.sub{height:30px; width:80px; border-radius:3px;
border:1.5px solid gray; font-size:15px; }
input{border-radius:3px;border:1.5px solid #ebebe0}
select{border-radius:3px;border:1.5px solid #ebebe0}
</style>
</head>
<body>
<div id="div1">
<h2 id="heading">Employee Details</h2>
<form action ="econnect.php" method="post">
<table>
<tr>
<td class="inpname">Employee ID :</td>
<td><input type="text" name="eid" value="" class="inp"></td>
<td class="inpname"> Employee Name :</td>
<td><input type="text" name="ename" value="" class="inp"></td>
</tr>
<tr>
<td class="inpname">Father's Name :</td>
<td><input type="text" name="fathername" value="" class="inp"></td>
<td class="inpname">Mother's Name :</td>
<td><input type="text" name="mothername" value="" class="inp"></td>
</tr>
<tr>
<td class="inpname">Gender :</td>
<td><selectname="gender"class="inp">
<option>---select---</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option></td>
<td class="inpname">City :</td>
<td><input type="text" name="City" value="" class="inp"></td>
</tr>
<tr>
<td class="inpname">Date of Birth :</td>
<td><input type="text" name="dob" value="" class="inp"></td>
<td class="inpname">Blood Group :</td>
<td><input type="text" name="bloodgroup" value="" class="inp"></td>
</tr>
<tr>
<td class="inpname">Mail ID :</td>
<td><input type="text" name="mailid" value="" class="inp"></td>
<td class="inpname">Phone Number :</td>
<td><input type="text" name="phonenumber" value="" class="inp"></td>
</tr>
<tr>
<td colspan="4" class="submit"><input class="sub" type="submit" value="Submit"></td>
</tr>
</table>
</form>
</div></body>
</html>
Output:
Step2: We have to create database called company under that we have to create a table called
EmployeeDetails with the above values.
EmployeeDetails with the above values.
Step3: For inserting data into this table we have to create econnect.php file and this file we have to
give in the html form action attribute.
give in the html form action attribute.
Econnect.php Code:
<?php
$serverName = "DESKTOP-T06U81T";
$connectionInfo = array( "Database"=>"company1",'UID'=>'sa', 'PWD'=>'spsm');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$eid=$_POST['eid'];
$ename=$_POST['ename'];
$fathername=$_POST['fathername'];
$mothername=$_POST['mothername'];
$gender=$_POST['gender'];
$City=$_POST['City'];
$dob=$_POST['dob'];
$bloodgroup=$_POST['bloodgroup'];
$mailid=$_POST['mailid'];
$phonenumber=$_POST['phonenumber'];
$tsql = sqlsrv_query($conn, "INSERT INTO EmployeeDetails (EmployeeID,EmployeeName,
FatherName,MotherName,Gender,City,DateOfBirth,BloodGroup,MailID,PhoneNumber) VALUES
('$eid','$ename','$fathername','$mothername','$gender','$City','$dob','$bloodgroup','$mailid',
'$phonenumber')");
FatherName,MotherName,Gender,City,DateOfBirth,BloodGroup,MailID,PhoneNumber) VALUES
('$eid','$ename','$fathername','$mothername','$gender','$City','$dob','$bloodgroup','$mailid',
'$phonenumber')");
echo "Data is stored successfully";
?>
Step4: Enter the data in EmployeeDetails form and then check the database whether the data is
inserted or not.
inserted or not.
Then click on submit the following message will be displayed.
Now we have to open sql server management studio and run the below query to check the data.
Output:
For practice we will insert the data once again.
Then click on submit the following message will be displayed.
Now we have to open sql server management studio and run the below query to check the data.
Output:
Retrieving data from the database through php
Step5: For retrieving data from the database through php first we need to do is we have to create a
new php file(vEmployee.php) and this must be added to econnect.php program in step3.
new php file(vEmployee.php) and this must be added to econnect.php program in step3.
Note: I am retrieving only few columns.
vEmployee.php
<html>
<head>
<style>
table,td,tr,th{border:1px solid gray;border-collapse:collapse;}
td,th {text-align:center; width:150px; padding:12px; height:50px;}
th {font-size:15px;}
td {font-size:14px;}
body{font-family:arial;}
div {width:80%;}
</style>
</head>
<body><center>
<?php
$serverName = "DESKTOP-T06U81T";
$connectionInfo = array( "Database"=>"company1",'UID'=>'sa', 'PWD'=>'spsm');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
echo "<div>";
echo"<h2 align=center>Employee List<h2>";
$query = "SELECT EmployeeID,EmployeeName,FatherName,MotherName,MailID,PhoneNumber
FROM EmployeeDetails ORDER BY EmployeeID";
FROM EmployeeDetails ORDER BY EmployeeID";
$result = sqlsrv_query( $conn,$query );
if( $result === false)
{
die( print_r( sqlsrv_errors(), true) );
}
echo "<table><tr><th>ID</th><th>Name</th><th>Father Name</th><th>Mother Name</th><th>Mail ID
</th><th>Phone Number</th></tr>";
</th><th>Phone Number</th></tr>";
while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo "<td>".$row['EmployeeID']."</td>";
echo "<td>".$row['EmployeeName']."</td>";
echo "<td>".$row['FatherName']."</td>";
echo "<td>".$row['MotherName']."</td>";
echo "<td>".$row['MailID']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
?></center>
</body>
</html>
Output:
Updating data into database:
Step6: For this case we have to add edit and delete options in each row when we are retrieving the data. For
this I have added two more columns to the retrieving list those are edit and delete
this I have added two more columns to the retrieving list those are edit and delete
vEmployee.php code:
<html>
<head>
<style>
table,td,tr,th{border:1px solid gray;border-collapse:collapse;}
td,th {text-align:center; width:150px; padding:12px; height:50px;}
th {font-size:15px;}
td {font-size:14px;}
body{font-family:sans-serif;}
div {width:80%;}
</style>
</head>
<body><center>
<?php
$serverName = "DESKTOP-T06U81T";
$connectionInfo = array( "Database"=>"company1",'UID'=>'sa', 'PWD'=>'spsm');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
echo "<div>";
echo"<h2 align=center>Employee List<h2>";
$query = "SELECT EmployeeID,EmployeeName,MailID,PhoneNumber FROM EmployeeDetails
ORDER BY EmployeeID";
ORDER BY EmployeeID";
$result = sqlsrv_query( $conn,$query );
if( $result === false)
{
die( print_r( sqlsrv_errors(), true) );
}
echo "<table><tr><th>ID</th><th>Name</th><th>Mail ID</th><th>Phone Number</th><th>edit</th><th>update</th></tr>";
while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) )
{
echo "<tr>";
echo"<td>".$row['EmployeeID']."</td>"; echo"<td>".$row['EmployeeName']."</td>";
echo "<td>".$row['MailID']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
echo "<td><form action='Edit.php?id=".$row['EmployeeID']."' method='post'>
<input class='button' type='hidden' name='id' value='".$row['EmployeeID']."'>
<input class='button' type='submit' name='edit' value='Edit'></form></td>";
echo "<td><form action='Delete.php?id=".$row['EmployeeID']."' method='post'>
<input class='button' type='hidden' name='id' value='".$row['EmployeeID']."'>
<input class='button' type='submit' name='delete' value='Delete'></form></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
?></center>
</body>
</html>
Output:
You can see the edit and delete options in the above list
Step7: Now we need to write edit.php and delete.php programs for above operations.
Edit.php code:
<?php
$serverName = "DESKTOP-T06U81T";
$connectionInfo = array( "Database"=>"company1",'UID'=>'sa', 'PWD'=>'spsm');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$id = $_POST['id'];
$query = "SELECT EmployeeID,EmployeeName,MailID,PhoneNumber FROM EmployeeDetails
WHERE EmployeeID = '$id'";
WHERE EmployeeID = '$id'";
$result = sqlsrv_query( $conn,$query );
while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) )
{
echo "<html>";
echo "<head>";
echo "<style>
body{font-family:sans-serif;}
td {width:150px; padding:5px;}
.inp{width:250px; height:25px;}
#div1{width:850px; }
#heading{text-align:center;}
.inpname{text-align:right; font-size:14;}
.submit {text-align:right;}
.sub{height:30px; width:80px; border-radius:4px;
border:1px solid gray; font-size:15px; }
</style>";
echo "</head>";
echo "<body>";
echo "<div id='div1'>";
echo "<h2 id='heading'>Edit Employee Details</h2>";
echo "<form action='Update.php' method='post'>";
echo "<table>";
echo "<tr>";
echo "<td class='inpname'>Employee ID :</td>";
echo "<td><input type='text' name='neweid' value=".$row['EmployeeID']." class='inp'></td>";
echo "<td class='inpname'>Employee Name :</td>";
echo "<td><input type='text' name='newename' value=".$row['EmployeeName']." class='inp'></td>";
echo "</tr>";
echo "<tr>";
echo "<tr>";
echo "<td class='inpname'>Mail ID :</td>";
echo "<td><input type='text' name='newmailid' value=".$row['MailID']." class='inp'></td>";
echo "<td class='inpname'>Phone Number :</td>";
echo "<td><input type='text' name='newphonenumber' value=".$row['PhoneNumber']." class='inp'></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='4' class='submit'>
<input class='sub' type='submit' value='Update'>
<input class='button' type='hidden' name='id' value='".$row['EmployeeID']."'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "</div>";
echo "</body>";
echo "</html>";
}
?>
When i click on the edit button it has to show me the employee details form. When we made changes under
that it has to be updated. For updating i have written update.php program this has to be added to the
edit.php under form action.
that it has to be updated. For updating i have written update.php program this has to be added to the
edit.php under form action.
Update.php code:
<?php
$serverName = "DESKTOP-T06U81T";
$connectionInfo = array( "Database"=>"company1",'UID'=>'sa', 'PWD'=>'spsm');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$id = $_POST['id'];
$neweid=$_POST['neweid'];
$newename=$_POST['newename'];
$newmailid=$_POST['newmailid'];
$newphonenumber=$_POST['newphonenumber'];
$query = "UPDATE EmployeeDetails SET EmployeeID = '$neweid',EmployeeName = '$newename',
MailID = '$newmailid',PhoneNumber = '$newphonenumber'
MailID = '$newmailid',PhoneNumber = '$newphonenumber'
WHERE EmployeeID = '$id'";
//$tsql = "INSERT INTO EmployeeDetails (EmployeeID,EmployeeName,MailID,PhoneNumber)
VALUES ('$neweid','$newename','mailid','$newphonenumber')
VALUES ('$neweid','$newename','mailid','$newphonenumber')
WHERE EmployeeID = '$id' ";
$result = sqlsrv_query($conn, $query);
echo "Data is updated successfully";
echo "<script>location.href='vEmployee.php';</script>";
?>
Here I am updating my mail see the result below how it will be changing and displaying new data.
When i click on the update button it shows the following result.
This also we can check in our database.
Deleting of data from database through php
Step8: When you click on the delete button in employee list it has to delete the data from database.
Click on the delete button.
Total summary of this process:
Comments
Post a Comment