0% found this document useful (0 votes)
2 views

complete

The document contains a series of PHP programming exercises, each demonstrating different functionalities such as displaying messages, performing arithmetic operations, creating tables, and handling user input through forms. It includes examples of loops, conditional statements, arrays, and string functions. Each exercise is accompanied by corresponding HTML code to facilitate user interaction.

Uploaded by

Arshdeep Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

complete

The document contains a series of PHP programming exercises, each demonstrating different functionalities such as displaying messages, performing arithmetic operations, creating tables, and handling user input through forms. It includes examples of loops, conditional statements, arrays, and string functions. Each exercise is accompanied by corresponding HTML code to facilitate user interaction.

Uploaded by

Arshdeep Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

1) Write a program in PHP to display the message “This is PHP Page”.

<html>

<body>

<?php

echo"This is a PHP page";

?>

</html>

2) Write a program to print Table of 2.

<?php

for($i=1;$i<=10;$i++)

echo"2 *$i=".(2*$i)."<br>";

?>

1
3) Write a program to print a Table of any Number.

<?php

for($i=1;$i<=10;$i++)

echo"5 *$i=".(5*$i)."<br>";

?>

4) Write a program to create a Table with Colors name and their respective colors.

<html>

<body>

<table>

<tr>

<td>pink</td>

<tdstyle="background-color:pink;width:50%"></td>

</tr>

<tr>

<td>

<?php echo'red';

?>

2
</td>

<tdstyle="background-color:

<?php echo'red'

?>;width:

<?php

echo'50%'?>"></td>

</tr>

<?php

echo'<tr><td>blue</td><tdstyle="background-color:blue;width:50%"></td></tr>';

?>

</table>

</body>

</html>

5) Write a program to enter two Number and display the greatest one.

<html>

<body>

<form method="POST"action="bigger.php">

first number : <input type = "text" name = "num1"><br> second

number:<input type= "text"name="num2"><br>

<input type="submit"value="Greatest"><br>

3
</form>

</body>

</html>

PHP CODE

<html>

<body>

<?php

$x=$_POST["num1"];

$y=$_POST["num2"];

if($x>$y)

echo"$x is greater";

else

echo"$y is greater";

?>

</body>

</html>

10) Write a program to display a message corresponding to the day entered by the user using if-else
statement.

4
<html>

<body>

<form method="POST" action="days.php">

Enter day of week<input type="num" name="num">

<input type="submit" name="click">

</form>

</body>

</html>

PHP CODE

<html>

<body>

<?php

$n=$_POST["num"];

if($n=="monday")

echo("gotoschool");

elseif

($n=="tuesday")

echo("havefun");

elseif

($n=="wednesday")

echo("exercise");

}
5
Else if

($n=="thursday")

echo("studywell");

else

echo("ENJOY

");

?>

</body>

</html>

7)Write a program to display a message corresponding to the day entered by the user using switch

statement.

<?php

$d=$_POST['num'];

switch ($d)

{
6
case "Monday":

echo"today is monday";

break;

case "Tuesday":

echo"today is tuesday";

break;

case "Wednesday":

echo"today is wednesday";

break;

case "Thursday":

echo"today is thursday";

break;

case "Friday":

echo"today is friday";

break;

case "Saturday":

echo"today is saturday";

break;

case "Sunday":

echo"today is sunday";

break;

default:

echo"wonder which day is this";

?>

HTML CODE

<body>

7
<form method="POST" action="7.php">

Enter day of week<input type="num" name="num">

<input type="submit" name="click">

</form>

</body>

</html>

8) Write a program to check whether the number entered by the user is positive or negative.

<html>

<body>

<?php

$number=$_POST['num'];

if ($number > 0)

echo"<p>$number is a positive number";

Else if($number<0)

8
echo"<p>$number is a negative number";

else

echo"The number is zero";

?>

</body>

</html>

HTML CODE

<body>

<form method="POST" action="8.php">

Enter the number<input type="num" name="num">

<input type="submit" name="click">

</form>

</body>

</html>

9) Write a program to check whether the number entered by the user is even or odd using if-else
statement.

9
<html>

<body>

<?php

$number=$_POST['num'];

if($number%2==0){

echo"this number is

even:".$number."<br>";

}else{

echo"this number is odd:".$number."<br>";}

?>

</body>

</html>

HTML CODE

<body>

<form method="POST" action="9.php">

Enter the number<input type="num" name="num">

<input type="submit" name="click">

</form>

</body>

</html>

10) Write a program to check whether the number entered by the user is even or odd using switch
statement.
10
<html>

<body>

<?php

$number=$_POST['num'];

switch ($number % 2)

case 0:

echo"$number is even.";

break;

case 1:

echo"$number is odd.";

break;

?>

</body>

</html>

HTML CODE

<body>

<form method="POST" action="10.php">

Enter the number <input type="num" name="num">

<input type="submit" name="click">

</form>

</body>

</html>

11) Write a program to display a table whose number of rows and columns are entered by the user.
11
<body>

<form method="POST" action="11.php">

enter the rows<input type="num" name="num"><br>

enter the columns <input type="num"

name="num"><br>

<input type="submit" name="click">

</form>

</body>

</html>

PHP CODE

<html>

<body>

<?php

$rows=$_POST['num'];

$columns=$_POST['num'];

echo"<table>";

for($i=1;$i<=$rows; $i++)

echo"<tr>";

for($j=1;$j <=$columns;$j++)

echo"<td>Row$i,Col $j</td>";

echo"</tr>"; }

echo"</table>";

?>
12
</body>

</html>

12) Write a program to enter rupees in textbox and convert it into dollars using PHP.

<?php

$rupee=$_POST["num"];

$dollar=$rupee*84;

echo $dollar;

?>

HTML CODE

<html>

<body>

<form method="POST" action="12.php">

Enter the rupee<input type="text" name="num"><br>

<input type="submit"value="click">

</form>

</body>

</html>

13) Write a program to enter a temperature in Celsius in textbox and convert it into Fahrenheit
using PHP

13
<html>

<body>

<form method="POST" action="13.php">

Enter value in Celsius <input type="text" name="num"><br>

<input type="submit" value="enter">

</form>

</body>

</html>

PHP CODE

<html>

<body>

<?php

$a=$_POST["num"];

$b=($a*(9/5))+32;

echo $b;

?>

</body>

</html>

14) Write a program to enter a color in textbox and using PHP make that color as the background.

14
<?php

$col=$_POST["color"];

?>

<bodystyle="backgroud

color: <?php echo $col;

?>">

</body>

</html>

HTML CODE

<html>

<body>

<form method="post" action="14.php">

Enter the color<input type="text" name="color"><br>

<input type="submit" value="submit">

</form>

</body>

</html>

15) Write a program to set the colour of div (colour taken from user).

15
<html>

<head>

<title>Set Div Color</title>

</head>

<body>

<form method="post">

<label for="color">Choose a color:</label>

<input type="text"id="color"name="color"placeholder="e.g.,red,#ff0000">

<button type="submit">SetColor</button>

</form>

<divstyle="width:200px;height:100px;background-color:<?phpechoisset($_POST['color'])?

htmlspecialchars($_POST['color']) : 'lightgray'; ?>;">

This is a colored div.

</div>

</body>

</html>

16) Write a program to calculate the factorial of a number entered by the user using for loop.

16
<html>

<body>

<form method="POST" action="16.php">

Enter your number<input type="number" name="num"><br>

<input type="submit" value="click"><br>

</form>

</body>

</html>

PHP CODE

<html>

<body>

<?php

$fact=1;

$num=$_POST['num']; for($i=1;$i<=$num;$i++)

$fact=$fact*$i;

echo"factorial of a number is:",$fact;

?>

</body>

</html>

17) Write a program to calculate the factorial of a number entered by the user using while loop.

17
<?php

$number=5;

if($number<0)

echo"Please enter a non-negative integer.\n";

else{

$factorial=1;

$count=1;

while($count<=$number)

$factorial=$factorial*$count;

$count++;

echo"The factorial of $number is $factorial.\n";

?>

HTML CODE

<html>

<body>

<form method="POST"action="17.php">

Enter your number <input type="number"name="num"><br>

<input type="submit"value="click"><br>

</form>

</body>

</html>

18
18) Write a program to find factorial of the number entered by the user, using do-while loop.

<?php

$number=$_POST['num'];

if ($number < 0)

echo"Please enter a non-negative integer.\n";

else

$factorial=1;

$count=1; do

$factorial=$factorial*$count;

$count++;

}while($count<=$number);

echo"The factorial of $number is $factorial.\n";

?>

HTML CODE

<html>

<body>

<form method="POST"action="18.php">

Enter your number<input type="number"name="num"><br>

<input type="submit"value="click"><br>

</form>

</body>

19
</html>

19) Write a program to print the fibonacci series of a number enetered by the user.

<?php

$n=$_POST['num'];

function fibo($n)

if($n<=1)

Return $n;

else

return(fibo($n-1)+fibo($n-2));

}
for($i=0;$i<10;$i++)

{
echo fibo($i)."<br>";

?>

HTMl
CODE :

<html>

<body>

<form method="POST" action="19.php">

Enter your number <input type="number" name="num"><br>

<input type="submit" value="click"><br>

</form>

20
</body>

</html>

20) Write a program to create the following pattern:-

<?php

for($i=0;$i<=5;$i++)

for($j=1;$j<=$i;$j++)

Echo $j;

echo"<br>";

?>

21
21) Write a program to create the following pattern:-

<html>

<body>

<?php

for($i=5;$i>=1;$i--)

for($j=1;$j<=$i;$j++)

echo$j;

echo("<br>");

?>

</body>

</html>

22
22) Write a program to create the following pattern:-

<html>

<body>

<?php

for($i=1;$i<=9;$i++)

echo'1';

echo'<br>';

for($i=1;$i<=9;$i++)

echo'1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1';

echo'<br>';

for($i=1;$i<=9;$i++)

23
echo'1';

?>

</body>

</html>

23) Write a program to create the following pattern:-

<html>

<body>

<?php

24
for($i=1;$i<=5;$i++)

for($j=1;$j<=5-$i;$j++)

echo'&nbsp;&nbsp';

for($j=1;$j<=$i;$j++)

echo$j;

for($j=$i-1;$j>=1;$j--)

echo$j;

echo'<br>';

for($i=4;$i>=1;$i--)

for($j=5-$i;$j>=1;$j--)

echo'&nbsp;&nbsp';

for($j=1;$j<=$i;$j++)

echo$j;

for($j=$i-1;$j>=1;$j--)

25
{

echo$j;

echo'<br>';

?>

</body>

</html>

24) Write a program to perform arithmetic operations on two Number.

<?php

$num1=5;

$num2=5;

$num3=$num1+$num2;

$num4=$num1-$num2;

$num5=$num1/$num2;

$num6=$num1*$num2;

$num7=$num1%$num2; echo

$num3."<BR>"; echo $num4."<Br>";

echo $num5."<BR>"; echo

$num6."<BR>"; echo $num7;

?>

25) Write a program to create Array.

<?php

$bestie=array('sahil','aman','arsh','palak','ankita','unnati');

26
foreach( $bestie as $a )

Echo $a."<br>";

?>

26) write a program to create associative array.

<html>

body>

<?php

$besties=array

'a'=>'amanjeet',

'b'=>'arshdeep

27
'c'=>'palak');

Echo $besties['a']."<br>";

Echo$besties['b']."<br>;

echo $besties['c'];

?>

</body>

</html>

27) Write a program to print the sum of elements of an indexed array.

<?php

$numbers=array(10,20,30,40);

$sum="";

foreach($numbers as $value)

$sum+=$value;

echo"The sum is".$sum;

?>

28
28) Write a program to convert the string entered by the user into an array.

<?php

$string="arsh";

$array=str_split($string);

foreach ($array as $a)

Echo $a."<br>";

?>

29) Write a program to use Array Functions.

<?php

$student = array("arsh","aman","palak");

$arrlength=count($student);

echo current($student)."<br>";

echo next($student)."<br>";

echo prev($student)."<br>";

for($x=0;$x<$arrlength;$x++

echo $student[$x];

echo "<br>";

29
}

echo

reset($student)."<br>";

echo

end($student)."<br>";

?>

30) Write a program to display array element using Array Iterator

PHP CODE

<?php

$course=array("php"=>"bca","business"=>"bba","information"=>"btech tech");

$iteration=new Array iterator($course);

$iteration->rewind();

while($iteration->valid())

echo $iteration->current() ." has " . $iteration->key() ."<br>";

$iteration->next();

30
31. Write a program to Create Multidimensional Array and Display its elements.

<?php

$marks=array("

arsh"=>array(

"Php" => 80,

"Maths"=>50,

"java"=>99),

"aman"=>array(

"Php" => 70,

"Maths"=>92,

"java"=>79),

"palak"=>array(

"Php" => 41,

"Maths"=>62,

"java"=>89) );

echo "Marks of arsh in Php:";

echo $marks['arsh']['Php'].'<b';

echo"Marks of aman in

Maths:";

echo$marks['aman']['Maths'].'<b';

echo "Marks of palak in java:";

echo $marks['palak']['java'].'<br>';

?>

31
32. Write a program to use String Functions in PHP.

<?php

$str1="ArshdeepKaur";

$str2=empty($str1);

$str3="aman"; echo

strlen($str1); echo "<br>";

echo strrev ($str1);

echo"<br>";

echo str_repeat($str1,3);

echo "<br>";

echostrcmp($str1,$str3);

echo "<br>";

echostr_word_count($str1);

echo "<br>";

echostr_replace("palak","am

an",$str1); echo "<br>";

echotrim($str1);

echo "<br>";

echostrtolower($str1);

echo "<br>";
32
echo strtoupper($str1);

echo "<br>";

echoucfirst($str1);

echo "<br>";

echoucwords($str1);

echo "<br>";

?>

33) Write a Program to use Numeric Function.

<?php

$num1=3.4;

33
$num2=5;

echoceil($num1);

echo "<br>";

echo floor($num1);

echo "<br>";

echo abs($num1);

echo "<br>";

echo pow($num1,$num2);

echo "<br>";

echo log(4,2);

echo "<br>"; echo

exp(2);

echo"<br>";

echorand(2.5,2.0);

echo "<br>";

echobindec(1000);

echo "<br>";

echodecbin(15);

echo "<br>";

echo decoct(9);

echo "<br>";

echodechex(10);

echo "<br>";

echohexdec(15);

echo "<br>";
34
echooctdec(8);

echo "<br>";

printf("%4d",6);

echo "<br>";

?>

34. Write a program to create Registration form,insert,and display the values of the same usng PHP

<html>

<body>

<table border="solid 2px">

<form method="POST" action="34.php">

<tr><td>ID</td>

<td><input type="number" name="id"></td></tr>

<tr><td>Name</td>

<td><input type="text" name="txtname"></td></tr>

<tr><td>gender</td><td><input type="radio" name="gender" value="female">Female

<input type="radio" name="gender" value="male">Male</td></tr>

<tr><td>Hobbies</td>

<td><input type="checkbox" name="hobbies[]" value="singing ">singing


35
<input type="checkbox" name="hobbies[]" value="dancing ">dancing

<input type="checkbox" name="hobbies[]" value="swimming ">swimming

<input type="checkbox" name="hobbies[]" value="reading ">reading</td></tr>

<tr><td>Course</td><td><select name="course">

<option value="BCA1">BCA 1</option>

<option value="BCA2">BCA 2</option> <option

value="BCA3">BCA 3</option>

</select></td></tr>

<tr><td>Marks</td>

<td>English &nbsp;<input type="number" name="eng"><br>

Maths &nbsp;&nbsp;<input type="number" name="math"><br>

Hindi &nbsp;<input type="number" name="hin"><br>

Punjabi &nbsp;<input type="number" name="pun"><br>

SST &nbsp;<input type="number" name="sst"></td></tr>

<tr><td>Email</td>

<td><input type="text" name="txtemail"></td></tr>

<tr><td>Password</td>

<td><input type="password" name="txtpass"></td></tr>

<tr><td colspan="2" align="center"><input type="reset" value="reset">

<input type="submit" value="submit"></td></tr>

</form>

</table>

</body>

</html>

PHP CODE

<?php

echo $_POST["id"]."<br>";

36
echo $_POST["txtname"]."<br>";

echo $_POST["gender"]."<br>";

$arr=$_POST["hobbies"];

foreach($arr as $a){

echo $a . " ";}

echo "<br>";

echo $_POST["course"]."<br>";

echo $_POST["txtemail"]."<br>";

echo $_POST["txtpass"]."<br>";

$eng=$_POST["eng"];

$math=$_POST["math"];

$hin=$_POST["hin"];

$pun=$_POST["pun"];

$sst=$_POST["sst"];

$per=(($eng+$math+$hin+$pun+$sst)/500)*100;

echo $per;

?>

</body>

</html>

37
35) Write a program to create a table in database using PHP

<?php

$my=new mysqli("localhost","root","","arsh");

if($my==false)

die("error:couldnotconnecttodatabase");

$sql="CREATE table student(stu_id int(10),stu_name varchar(10),stu_age varchar(10))";

if($result=$my->query($sql))

echo"Table created!!!";

else

echo"Table not created!!!";

$my->close();

?>

36) Write a program to INSERT values in database table.

38
<?php

$mysqli=new mysqli("localhost","root","","arsh");

if($mysqli==false)

die("erroroccured");

$sql="INSERT INTO student(stu_id ,stu_name ,stu_age)

VALUES(1,'arsh',19)";

if($mysqli->query($sql)===true)

echo$mysqli->affected_rows.'rowupdates!!!';

Else

echo"error";

$mysqli->close();

?>

39
37) Write a program to DELETE values in a database table using PHP.

<html>

<body>

<?php

$mysqli=newmysqli("localhost","root","","arsh");

if($mysqli==false)

die("error occured");

$sql="DELETE from student WHERE stu_name='ankita'";

if($mysqli->query($sql)===true)

echo$mysqli->affected_rows. 'rowdeletes.!!!';

Else

echo"error";

$mysqli->close();

?>

</body>

</html>

40
38) Write a program to UPDATE values in a database table using PHP.

<?php

$mysqli=newmysqli("localhost","root","","arsh");

if($mysqli==false)

die("erroroccured");

$sql="UPDATE student SET stu_name = 'unnati', stu_age=19 WHERE stu_name= 'palak'";

if($mysqli->query($sql)===true)

Echo $mysqli->affected_rows. 'rowupdates.!!';

Else

echo"error";

$mysqli->close();

?>

39) Write a program to display all the values of database table using SELECT statement in PHP.

?php
41
$var=new mysqli("localhost","root","","arsh");

if($var===false)

die("ERROR: could not connect".mysqli_connect_error());

$qry="SELECT*from student";

if($result=$var->query($qry))

if($result->num_rows>0)

while($row=$result->fetch_array())

echo$row[0].":".$row[1].":".$row[2]."<br>";

$result->close();

else

echo"no records found";

else

echo"ERROR: could not execute $qry";

$var->close();

?>

42
40) Write a program using functions to display all the values of database table in a Table.

<?php

functiondata()

$ptr=new mysqli("localhost","root","","arsh");

if($ptr==false)

die("Error:could not connect database");

$sql="select * from student";

if($result=$ptr->query($sql))

if($result->num_rows>0)

display($result);

$result->close();

Else

{
echo"no matching records";

Else

{
echo"could not execute $sql";

}
43
$ptr->close();

}
functiondisplay($result)
{

while($rows=$result->fetch_array())

echo"<tableborder='2'width='200'><tr>";

echo "<td>$rows[0]</td>";

echo"<td>$rows[1]</td>";

echo"<td>$rows[2]</td>";

echo "</tr></table>";

data();

?>

41. Write a program to create a registration form and insert those values into database.

HTML CODE

<html>

<body>

44
<table border="solid2px">

<form method="POST" action="41.php">

<tr><td>ID</td>

<td><input type="number" name="id"></td></tr>

<tr><td>Name</td>

<td><input type="text" name="txtname"></td></tr>

<tr><td>gender</td><td><input type="radio" name="gender" value="female">Female

<input type="radio"name="gender"value="male">Male</td></tr>

<tr><td>Hobbies</td>

<td><input type="checkbox"name="hobbies[]" value="singing">singing

<input type="checkbox"name="hobbies[]" value="dancing">dancing

<input type="checkbox"name="hobbies[]" value="swimming">swimming

<input type="checkbox"name="hobbies[]" value="reading">reading</td></tr>

<tr><td>Course</td><td><select name="course">

<option value="BCA1">BCA1</option>

<option value="BCA2">BCA2</option>

<option value="BCA3">BCA3</option>

</select></td></tr>

<tr><td>Marks</td>

<td>PHP &nbsp;<input type="number" name="php"><br>

MATH&nbsp;&nbsp;

<input type="number"name="math"><br> JAVA &nbsp;

<input type="number" name="java"><br>

CN&nbsp;

<inputtype="number" name="cn"><br>

HISTORY&nbsp;

<input type="number " name="history"></td></tr>

45
<tr><td>Email</td>

<td><input type="text" name="txtemail"></td></tr>

<tr><td>Password</td>

<td><input type="password"name="txtpass"></td></tr>

<tr><tdcolspan="2"align="center">

<input type="reset"value="reset">

<input type="submit"value="submit"></td></tr>

</form>

</table>

</body>

</html>

PHP CODE

<?php

$id=$_POST["id"];

$name=$_POST["txtname"];

$gender=$_POST["gender"];

$hobbyarr=$_POST["hobbies"];

$hobby=implode("",$hobbyarr);

$course=$_POST["course"];

$email=$_POST["txtemail"];

$pass=$_POST["txtpass"];

$php=$_POST["php"];

$math=$_POST["math"];

$java=$_POST["java"];

$cn=$_POST["cn"];

$history=$_POST["history"];

$per=(($php+$math+$java+$cn+$history)/500)*100;

46
$sqlptr=newmysqli("localhost","root","","arsh");

if($sqlptr==false)

die("ERROR:connection not established");

$sql="Insert into studentinfo (ID,Name,Gender,Hobbies,Course,Email,Password,Percentage)

values ($id,'$name','$gender','$hobby','$course','$email',$pass,$per)";

if($sqlptr->query($sql)==true)

echo "New row inserted!!";

else{

echo"row not inserted!!";

$sqlptr->close();

?>

47
42) Write a program to insert the details of three tables (STUDENT,BOOK,AND ISSUE) in HTML
form and insert those values into database using functions.

HTML CODE

<html>

<body>

<form method ="POST" action = "Book.php">

<H3> STUDENT</H3>

STUDENT ID

<input type ="number" name="ID"><br><br>

STUDENT NAME

<input type ="text" name="name"><br><br>

STREAM

<select name="stream">

<option value=""></option

<option value="BCA">COMPUTER APPLICATION</option>

<option value="BBA">BBA</option>

<option value="BSC">BSC</option>

<option value="BCA">BCA</option>

</select><br><br>

SUBJECT

<input type ="text" name="subject"><br><br>

<h3>BOOK</h3>

BOOKID<br><br>

<input type ="number" name="bid"><br>

BOOK NAME<br><br>

<input type ="text" name="bname"><br>

BOOK AUTHOR<br><br>

<input type ="text" name="Bauthor"><br>

48
<h3>ISSUE</h3>

DATE OF ISSUE<br><br>

<input type ="date" name="date"><br><br>

<input type ="submit" value="insert data">

</form

</body>

</html>

PHP CODE

<html>

<body>

<?php

function retrieve ()

$mysqli = new mysqli("localhost","root","","aman");

if($mysqli == false)

die("error : could not connect");

addstudent($mysqli);

addbook($mysqli);

addissue($mysqli);

$mysqli->close ();

function addstudent ($mysqli)

$stuid=$_POST['ID'];

$stuname=$_POST['name'];

49
$stustream=$_POST['stream'];

$stusubject=$_POST['subject'];

$sql1 = "INSERT INTO student(stu_id,stu_name,stu_stream,stu_sub)

VALUE($stuid,'$stuname','$stustream','$stusubject')";

if($mysqli->query($sql1)===true)

echo "inserteddddddddd";}

else{

echo "not inserteddddddddddddddddddd";}

function addbook ($mysqli){

$bid=$_POST['bid'];

$bname=$_POST['bname'];

$author=$_POST['Bauthor'];

$sql2 = "INSERT INTO book(book_id,book_name,book_author)

VALUE($bid,'$bname','$author')";

if($mysqli->query($sql2)===true){

echo "inserteddddddddd";}

else{

echo "not inserteddddddddddddddddddd";}}

function addissue ($mysqli){

$stuid=$_POST['ID'];

$bid=$_POST['bid'];

$dateissue=$_POST['date'];

$sql3 = "INSERT INTO issue(stu_id,book_id,date_of_issue)

VALUE($stuid,$bid,'$dateissue')";

if($mysqli->query($sql3)===true){

50
echo "inserteddddddddd";}

else{

echo "not inserteddddddddddddddddddd";}

retrieve();

?>

</body>

</html>

43) Write a program to create a login page and validate the username and password.

HTML CODE

<html>

<body>

<form method="post" action="43.php">

USERNAME <input type="text" name="username"><br><br>


51
PASSWORD <input type="text" name="password"><br><br>

<input type="submit" value="submit">

</form>

</body>

</html>

PHP CODE

<?php

$name=$_POST["username"];

$pass=$_POST["password"];

$my=new mysqli("localhost","root","","arsh");

if($my==false)

die("error: couldnot connect to database");

$sql="select Password from logininfo where Username='$name'";

if($result=$my->query($sql))

if($result->num_rows==1)

while($rows=$result->fetch_array())

if(strcmp($rows[0],$pass)==0)

echo "login successfully";

else

52
echo "invalid password";

else

echo "invalid username";

else

echo "query not executed $sql";

$my->close();

?>

53
44) Write a program to create a cookie in PHP.

<html>

<body>

<?php

if(isset($_COOKIE['email']))

Echo 'welcomeback,'.$_COOKIE['email'].'!';

else { setcookie('email','[email protected]',mktime()+129600,'/');

echo 'cookie created';

?>

</body>

</html>

45) Write a program to check how many times you have visited cookie.

<?php

if(isset($_COOKIE ['VisitCounter']))
54
{

setcookie("VisitCounter",$_COOKIE['VisitCounter']+1,time()+86400000,"/");

echo 'welcome back, ' . $_COOKIE['VisitCounter'] .'!';

else { setcookie("VisitCounter","1",time()+86400000,"/"); echo 'Cookie created';

?>

46) Write a program to create a session for users.

HTML CODE

<html>

<body>

<form method="POST" action="46.php">

ENTER YOUR NAME:<input type = "text" name="txtname"><BR><BR>

<input type="submit" name ="sessioncreate" value="CREATESESSION">

<input type="submit" name ="sessiondestroy" value="DESTROYSESSION">

</body>

</html>
55
PHP CODE

<?php

$name=$_POST['txtname']; session_start();

if(isset($_POST['sessiondestroy']))

session_destroy();

echo "SESSION DESTROYED";

} else

if(isset($_SESSION['USERNAME']))

foreach($_SESSION['USERNAME'] as $u)

echo "Welcome Back!!".$u;

else

$_SESSION['USERNAME'][]=$name;

echo "Session is created!!!!.";

?>

56
47) Write a program to print visit time in using sessions.

<html>

<body>

<h1>project : tracking previous Visits to a page</h1>

<?php

session_start();

date_default_timezone_set ("Asia/Calcutta");

if(!isset ($_SESSION['visits']))

echo "this is your first visit";

else

echo "you have previously visited this page"; foreach($_SESSION['visits'] as $v)

echo date('D M Y h:i:s' .$v)."<br>";

$_SESSION['visits'][]=mktime();

?>

</body>

</html>

57
58
59

You might also like