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

Sample Code for Group Assignment

The document provides various PHP code samples for different tasks such as connecting to a database, creating a student registration form, displaying data in a table format, uploading images, and tracking page visits. Each section includes HTML and PHP code snippets along with expected output examples. The code demonstrates basic database operations, form handling, and session management in PHP.

Uploaded by

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

Sample Code for Group Assignment

The document provides various PHP code samples for different tasks such as connecting to a database, creating a student registration form, displaying data in a table format, uploading images, and tracking page visits. Each section includes HTML and PHP code snippets along with expected output examples. The code demonstrates basic database operations, form handling, and session management in PHP.

Uploaded by

seanahmed626
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Sample Code to Help

Aim: Write a PHP program to select data and show into table format.

<html>
<head>
<title>Create Database. </title>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("not opened");
}
echo "Connection open"."</br>";

$db = mysql_select_db("studinfo",$con);
if(!$db)
{
die("Database not found".mysql_error());
}
echo "Database is selected"."</br>";

$query = "select * from computer";


$sldt = mysql_query($query,$con);
if(!$sldt)
{
die("data not selected".mysql_error());
}
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Branch</th>
</tr>";
while($row = mysql_fetch_array($sldt))
{
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['branch']."</td>";
echo "</tr>";
}
echo "</table>";

?>
</body>
</html>

O/P:
Connection open
Database is selected
ID Name Branch
9 Anil J Basantani CE
9 Anil J Basantani CE
9 Anil J Basantani CE

Practical-19
AIM: - Create a student Registration in PHP and Save and Display the
student Records.

<html>
<head>
<title>general form</title>
</head>
<body bgcolor="aakk">
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name:
<input type = "text" name = "txtname">
<br><br>

Roll no.:
<input type = "text" name = "txtr_no">
<br><br>

Gender:
<input type = "text" name = "txtgen">
<br><br>
Address:
<textarea name = "add" type = "textarea"></textarea>
<br><br>

<input type = "Submit" name = "insert" value = "Save">


<input type = "Reset" value = "Cancle">
</form>
</body>
</html>
<?php
if(isset($_POST['insert']))
{
$con = mysql_connect("localhost","root","");
if($con)
{
echo "Mysql connection ok<br>";
mysql_select_db("studinfo",$con);

$name = strval($_POST['txtname']);
$rollno = intval($_POST['txtr_no']);
$gender = strval($_POST['txtgen']);
$address = strval($_POST['add']);

$insert = "insert into info values('$name',$rollno,'$gender','$address')";


if(mysql_query($insert,$con))
{
echo "Data inserted successfully<br>";
}

$query = "select * from info";


$sldt = mysql_query($query,$con);

echo "<table border='1'>


<tr>
<th>Name</th>
<th>Roll No</th>
<th>Gender</th>
<th>Address</th>
</tr>";
while($row = mysql_fetch_array($sldt))
{
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['rollno']."</td>";
echo "<td>".$row['gen']."</td>";
echo "<td>".$row['address']."</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
}
}
?>

O/P:
PAPER SOLUTION
Extra-1

AIM: - Write a program to Develop student registration form and display


all the submitted data on another page.

Form fill.php

<html>
<head>
<title>insert data in form</title>
</head>
<body>
<form action = "getdata.php" method = "POST">
Name:
<input type = "text" name = "txtname">
<br><br>

Roll no.:
<input type = "text" name = "txtr_no">
<br><br>

Address:
<textarea name = "add" type = "textarea"></textarea>
<br><br>

Contyact No:
<input type = "text" name = "txtc_no">
<br><br>

Email ID:
<input type = "text" name = "txteid">
<br><br>

<input type = "Submit" name = "insert" value = "Save">


<input type = "Reset" value = "Cancle">
</form>
</body>
</html>
Getdata.php

<html>
<head>
<title>get data from another page</title>
</head>
<body>

<?php

echo "Name:" .$_POST["txtname"]."</br>";


echo "Roll no.:".$_POST["txtr_no"]."</br>";
echo "Address:".$_POST["add"]."</br>";
echo "Contact No.:".$_POST["txtc_no"]."</br>";
echo "Email ID:".$_POST["txteid"]."</br>";

?>

</body>
</html>
O/P:
Extra-2
AIM: - Write a program to read customer information like c_no, c_name,
item_purchased and mob_no from customer table and display all this
information in table format on output screen.

<html>
<head>
<title>display data in table format</title>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("not connected".mysql_error());
}
echo "Connection open"."<br/>";
$sldb = mysql_select_db("coust",$con);
if(!$sldb)
{
die("not found".mysql_error());
}
echo "Database selected"."<br/>";
$query = "select * from customer";
$sql = mysql_query($query);

echo "<table border = '1'>


<tr>
<th>C_No</th>
<th>C_Name</th>
<th>Item_Purchased</th>
<th>Mob_no</th>
</tr>";
while($row = mysql_fetch_array($sql))
{
echo "<tr>";
echo "<td>".$row['c_no']."</td>";
echo "<td>".$row['c_name']."</td>";
echo "<td>".$row['item_purchased']."</td>";
echo "<td>".$row['mob_no']."</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

O/P:
Connection open
Database selected
C_No C_Name Item_Purchased Mob_no
1 Anil Book 2147483647
2 Yogesh Marker 2147483647

Extra-3
AIM: - Write PHP code to upload image.

<?php

$result=0;

if (trim($_POST["action"]) == "Upload File")


{
$imagename = basename($_FILES['image_file']['name']);

$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $imagename);


if ($result==1) echo("Successfully uploaded: <b>".$imagename."</b>");

}
?>
<html>

<head>

<title>Upload file script</title>

</head>
<body>

<form method='POST' enctype='multipart/form-data' name='frmmain' >

Browse for Image (jpg): <input type="file" name="image_file" size="35">


<br>

<input type="submit" value=" Upload File " name="action">

</form>
<br>

<?php

if ($result==1) echo("<img src='".$imagename."'>");

?>

</body>

</html>

O/P
Browse for Image (jpg): Upload
Upload File
Extra-4
AIM: - Write a program that keeps track of how many times a visitor has
loaded the page.

<html>
<head>
<title>php counter</title>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['counter']))
{
$_SESSION['counter'] += 1;
}
else
{
$_SESSION['counter'] = 1;
}
echo "You have visited this page ".$_SESSION['counter']." time in this session";
?>
</body>
</html>

O/P:
You have visited this page 1 time in this session
Extra-5
AIM: Write a program that displays a different message based on
time of day. For example page should display “Good Morning” if it is
accessed in the morning.
<?PHP
//Change the messages to what you want.
$afternoon = "Good afternoon! ";
$evening = "Good evening! ";
$late = "Working late? ";
$morning = "Good morning! ";
$friday= "Get ready for the weekend! ";
//Get the current hour
$current_time = date('G');

//Get the current day


$current_day = date('l');

//12 p.m. - 4 p.m.


if ($current_time >= 12 &&
$current_time <= 16) { echo $afternoon;
}
// 5 p.m. to 11 p.m.
elseif ($current_time >= 17 &&
$current_time <= 24) { echo $evening;
}
//12 a.m. - 5 a.m.
elseif ($current_time >= 1 && $current_time
<= 5) { echo $late;
}
// 6 a.m. to 11 a.m.
elseif ($current_time >= 6 &&
$current_time <= 11) { echo $morning;
}
//If it's Friday, display a
message if
($current_day ==
"Friday")
{
echo $friday;
}

?>

O/P :-
Good morning! (Its base on Current Time)

You might also like