PHP Lab Programs V Sem
PHP Lab Programs V Sem
<html>
<head>
<title>name</title>
</head>
<body>
<?php
echo "<b><i>Hello</b> </i>";
?>
</body>
</html>
Output:
2. Write a PHP Program to display the today’s date.
<html>
<head>
<title>name</title>
</head>
<body>
<?php
$currentDateTime = date('Y-m-d, H:i:s');
echo "Today's date and time is:" .$currentDateTime;
?>
</body>
</html>
Output:
3. Write a PHP program to display Fibonacci series.
<html>
<head>
<title>Fibonacci series</title>
</head>
<body>
<?php
$n = 0;
$a = 0;
$b = 1;
echo "Fibonacci series is: ";
echo "$a, $b";
while ($n < 5 )
{
$c = $b + $a;
echo ", ";
echo "$c";
$a = $b;
$b = $c;
$n = $n + 1;
}
?>
</body>
</html>
Output:
4. Write PHP script to demonstrate passing variables with cookies.
<html>
<head>
<title>Cookie Demo</title>
</head>
<body>
<form method="post" action="cookies.php">
Enter Your Name :
<input type="text" name="fname" value="<?php echo $_COOKIE['myname']; ?>">
<input type="submit" value="Create Cookie" name="submit">
</form>
</body>
</html>
Output:
<?php
if(isset($_POST['submit']))
{
$name=$_POST['fname'];
setcookie('myname',$name,time()+100,"/","",0);
echo "Your name is : ".$_COOKIE['myname'];
if(!isset($_COOKIE['myname']))
{
echo "failed to create cookie";
}
}
?>
Output:
5. Write a program to keep track of how many times a visitor has loaded the page.
<html>
<head>
<title>visitors count</title>
</head>
<body>
<?php
$handle = fopen("counter.txt", "r"); //create a counter.txt file first
if(!$handle)
{
echo "could not open the file" ;
}
else
{
$counter = (int ) fread($handle,20);
fclose ($handle);
$counter++;
echo" <strong> you are visitor no ". $counter . " </strong> " ;
$handle = fopen("counter.txt", "w" );
fwrite($handle,$counter);
fclose($handle);
}
?>
</body>
</html>
Output:
6. Write a PHP application to add new Rows in a Table.
<html>
<head>
<title>employee details</title>
</head>
<body>
<h1 align="center" class="style2 style1">Employee Details</h1>
<form method="post" action="empins.php">
<p> </p>
<table border="0" width= 24% align=center>
<tr><td width="48%"><strong>Employee Id</strong></td>
<td width="52%"><input type="text" name="eid" required autofocus="true">
</td></tr>
<tr><td><strong>Employee Name</strong></td>
<td><input type="text" name="ena"></td></tr>
<tr><td><strong>Age</strong></td>
<td><input type="text" name="age"></td></tr>
<tr><td><strong>Salary</strong></td>
<td><input type="text" name="sal"></td></tr>
</table>
<table width="24%" border="0" align="center">
<tr>
<td width="44%">
<div align="right">
<input type="submit" name="submit" value="submit">
</div></td>
<td width="56%">
<div align="left">
<input type="reset" name="reset" value="reset">
</div></td></tr>
</table>
<p> </p>
</form>
</body>
</html>
Output:
<?php
include_once("connect.php");
if(isset($_POST['submit']))
{
$eid = $_POST['eid'];
$ena = $_POST['ena'];
$age = $_POST['age'];
$sal = $_POST['sal'];
$validate = "insert into `employee` (eid,ena,age,sal)values('$eid','$ena','$age','$sal')";
$query_validate = mysqli_query($conn,$validate);
if($query_validate)
{
echo "<br><br><br><br><br><br><br><br><center><b>Record Inserted successfully</b></center>";
}
}
?>
Output:
7. Write a PHP application to modify the Rows in a Table.
<html>
<head>
<title>employee details</title>
</head>
<body>
<h1 align="center" class="style2 style1">Employee Details</h1>
<form method="post" action="empins.php">
<p> </p>
<table border="0" width= 24% align=center>
<tr><td width="48%"><strong>Employee Id</strong></td>
<td width="52%"><input type="text" name="eid" required autofocus="true">
</td></tr>
<tr><td><strong>Employee Name</strong></td>
<td><input type="text" name="ena"></td></tr>
<tr><td><strong>Age</strong></td>
<td><input type="text" name="age"></td></tr>
<tr><td><strong>Salary</strong></td>
<td><input type="text" name="sal"></td></tr>
</table>
<table width="24%" border="0" align="center">
<tr>
<td width="44%">
<div align="right">
<input type="submit" name="submit" value="submit">
</div></td>
<td width="56%">
<div align="left">
<input type="reset" name="rubmit" value="reset">
</div></td></tr>
</table>
<p> </p>
</form>
</body>
</html>
Output:
<?php
include_once("connect.php");
if(isset($_POST['submit']))
{
$db_eid = $_POST['eid'];
$db_sal= $_POST['sal'];
$update = "update `employee` set sal='$db_sal' where eid='$db_eid'";
$query_update = mysqli_query($conn,$update);
if($query_update)
{
echo "<br><br><br><br><br><br><br><br><center><b>Record Updated successfully</b></center>";
}
}
?>
Output:
8. Write a PHP application to delete the Rows from a Table.
<html>
<head>
<title>employee details</title>
</head>
<body>
<h1 align="center" class="style2 style1">Employee Details</h1>
<form method="post" action="empins.php">
<p> </p>
<table border="0" width= 24% align=center>
<tr><td width="48%"><strong>Employee Id</strong></td>
<td width="52%"><input type="text" name="eid" required autofocus="true">
</td></tr>
<tr><td><strong>Employee Name</strong></td>
<td><input type="text" name="ena"></td></tr>
<tr><td><strong>Age</strong></td>
<td><input type="text" name="age"></td></tr>
<tr><td><strong>Salary</strong></td>
<td><input type="text" name="sal"></td></tr>
</table>
<table width="24%" border="0" align="center">
<tr>
<td width="44%">
<div align="right">
<input type="submit" name="submit" value="submit">
</div></td>
<td width="56%">
<div align="left">
<input type="reset" name="reset" value="reset">
</div></td></tr>
</table>
<p> </p>
<label></label>
<label></label>
</form>
</body>
</html>
Output:
<?php
include_once("connect.php");
if(isset($_POST['submit']))
{
$db_eid = $_POST['eid'];
$delete = "delete from `employee` where eid='$db_eid'";
$query_delete = mysqli_query($conn,$delete);
if($query_delete)
{
echo "<br><br><br><br><br><br><br><br><center><b>Record Deleted successfully</b></center>";
}
}
?>
Output:
9. Write a PHP application to fetch the Rows in a Table.
<html>
<head>
<title>employee details</title>
</head>
<body>
<h1 align="center" class="style2 style1">Employee Details</h1>
<form method="post" action="empins.php">
<p> </p>
<table border="0" width= 24% align=center>
<tr><td width="48%"><strong>Employee Id</strong></td>
<td width="52%"><input type="text" name="eid" required autofocus="true">
</td></tr>
<tr><td><strong>Employee Name</strong></td>
<td><input type="text" name="ena"></td></tr>
<tr><td><strong>Age</strong></td>
<td><input type="text" name="age"></td></tr>
<tr><td><strong>Salary</strong></td>
<td><input type="text" name="sal"></td></tr>
</table>
<table width="24%" border="0" align="center">
<tr>
<td width="44%">
<div align="right">
<input type="submit" name="submit" value="submit">
</div></td>
<td width="56%">
<div align="left">
<input type="reset" name="reset" value="reset">
</div></td></tr>
</table>
<p> </p>
<label></label>
<label></label>
</form>
</body>
</html>
Output:
<?php
include_once("connect.php");
if(isset($_POST['submit']))
{
$db_eid = $_POST['eid'];
$db_sal= $_POST['sal'];
$update = "update `employee` set sal='$db_sal' where eid='$db_eid'";
$query_update = mysqli_query($conn,$update);
if($query_update)
{
echo "<br><br><br><br><br><br><br><br><center><b>Record Updated successfully</b></center>";
}
}
?>
Output:
10. Write a PHP script to connect MySQL server from your website.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn)
{
die("Connection failed:".mysqli_connect_error());
}
echo "Connected successfully";
?>
Output:
11. Write a program to edit name of customer to “Kiran” with cust-no =1, and to delete record with cust-
no=3.