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

PHP Spiral

The document is a project report on web application development submitted to K.K Shastri Government BCA College. It contains an index listing 10 topics covered in Unit 1 of the project. The topics demonstrate basic PHP concepts like using comments, echo, print, if/else statements, switch statements, arrays, functions, and sorting/searching arrays. Unit 2 topics include creating forms to insert, update, delete records in a database table for a product using the HTTP GET/POST methods. The report is submitted by student Pathan Rahema S. under the guidance of professor Sujatha Mudhaliyar.

Uploaded by

Rahema Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

PHP Spiral

The document is a project report on web application development submitted to K.K Shastri Government BCA College. It contains an index listing 10 topics covered in Unit 1 of the project. The topics demonstrate basic PHP concepts like using comments, echo, print, if/else statements, switch statements, arrays, functions, and sorting/searching arrays. Unit 2 topics include creating forms to insert, update, delete records in a database table for a product using the HTTP GET/POST methods. The report is submitted by student Pathan Rahema S. under the guidance of professor Sujatha Mudhaliyar.

Uploaded by

Rahema Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

A PROJECT REPORT ON

WEB APPLICATION DEVELOPMENT ||


CC-310 PRACTICALS

SUBMITTED TO

K.K SHASTRI GOVERNMENT BCA COLLEGE

SUBMITTED BY

PATHAN RAHEMA S. ROLL NO:343

UNDER THE GUIDANCE OF

PROF. SUJATHA MUDHALIYAR

SIGNATURE:

____________________

1
INDEX
NO TOPIC SIGN
UNIT-1
Display "learning php" in bold format in PHP.
1
Demonstrate the use of comments, echo, and print in PHP.
2
Demonstrate the use of if...else and switch statements in PHP.
3
Create and display elements of an array using for loop and foreach
4 statement in PHP.
Create an array named $student, with 5 elements bound to different keys
5 and access using keys.
Demonstrate the use of multidimensional arrays in PHP.
6
Create two functions in PHP, parameterized and non-parameterized, for
7 implementing string concatenation.
Write a PHP program to display information about PHP in the browser.
8
Write a program in PHP to sort the array of given 5 numbers
9 in ascending and descending order.
Write a program to count the total number of times a
10 specific values appears in an array.

UNIT-2

.create a form containing two input fields(name, email_id)and a submit


1 button. when the user clicks on submit button ,the form data should
be sent for processing to php file, which should display the welcome
message with the email_id on the php page. form data should be sent
by http GET/POST method.
write a php script for that creates a database named “DB-1” in MYSQL.
2
.write a php script for creating a product table in the specified
3 database with fields prod-id,prod_name,prod_name,prod_price,QOH.
Also display an acknowledgement for the same.
create a form containing four input
4 fields(prod_id,prod_name,prod_price,QOH) and submit button. When
the user clicks on the submit button an php script should be executed
which inserts the record in the product table.

2
create a form containing one input field(prod_id) and a search button.
5 When the user clicks on the search button a php script should get
executed and should display the details of the product for the prod_id
specified.
create a form containing two input fields (prod_id,QOH) and update
6 button.when user clicks on the update button the quanitity of the
prod_id specified should get updated using a php script.
create a form containing one input field(prod_id) and a delete button.
7 When the user clicks on the delete button a php script should get
executed aand should delete the record of the product for prod_id
specified.

3
UNIT-1

1.write a program in php to display "learning php" in bold format.

<?php

echo "<b>hello</b><br>";
echo "<b>learning php</b>";

?>

2.write a program in php to demostrate the use of comments,echo and print.

<?php
echo "below is the example of comments in php</br>";
//echo "this the example of single line comment";
/*
multiline comment example:
$name=rahema;
echo $name;
*/
print "here we are using print statement>";
?>

3.create a program in php to demostrate the use of if...else and switch


statements.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>if-else example</title>
</head>
<body>
<?php

4
$age=19;
$gender="f";
if($age>18)
{
if($gender=='m'||$gender=='M')
{
echo "you are eligible for vote ,you are male candidate and your
age is above 18<br>";
}
else
{
echo "you are eligible for vote ,you are female candidate and your
age is above 18<br>";
}
}
else
{
echo "you are not eligible for voting since your age is below 18<br>";
}

?>
</body>
</html>

*.switch-case:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>if-else example</title>
</head>
<body>
<?php
$age=19;
$gender="f";

5
switch($gender)
{
case 'm':
echo "you are eligible for vote ,you are male candidate and your
age is above 18<br>";
break;
case 'f':
echo "you are eligible for vote ,you are female candidate and your
age is above 18<br>";
break;
default:
echo "you are not eligible for voting<br>";
}

?>
</body>
</html>

4.create an array named $sub ,assign five elements to i and display the
elemens assigned using for loop and for each statement.

<?php
$sub=array(1,2,3,4,5,55,66,77,88);
$cnt=count($sub); //count func will give total num of elements

for($i=0;$i<$cnt;$i++)
{
echo "<br>arr[".$i."]=".$sub[$i];

}
foreach($sub as $val)
{
echo "<br>".$val;

6
}
$m=array("banana",11,333.333,'a');
print_r($m);
for($j=0;$j<count($m);$j++)
{
echo "<br>arr[".$j."]=".$m[$j];

}
?>
?>

5.create an array named $student, that scores 5 elements bounded to a


different keys and access the same using the key element.

<?php
$student=array("name"=>"hima
patel","age"=>5,"height"=>3.4,"city"=>"ahmedabad");
echo $student["name"]."<br>";
echo $student["age"]."<br>";
echo $student["height"]."<br>";
echo $student["city"]."<br>";
foreach($student as $key=>$val)
{

7
echo "<br>[".$key."]=".$val;

?>

6.write a program in php to demostrate the use of multidimensional arrays.


<?php
$shop=array("mobile"=>array("apple"=>"i12","samsung"=>"m12","mi"=>"note9
pro"),
"laptop"=>array("lenovo"=>"z350","dell"=>"insp12","hp"=>"hp2200"));
echo $shop["mobile"]["apple"]."<br>";
echo $shop["mobile"]["mi"]."<br>";
echo $shop["laptop"]["hp"]."<br>";
echo $shop["mobile"]["samsung"]."<br>";

?>

8
7.create two functions in php,parametereized and non-parameterized forr
implementing string concatenation operation.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example 7</title>
</head>
<body>
<form name="add" method="post" action="function.php">
<label>fname</label><input type="text" name="fname" value=""/><br>
<label>lname</label><input type="text" name="lname" value=""/><br>
<input type="submit" name="submit" value="submit"/><br>

</form>
</body>
</html>

Function.php

<?php
function string_cat(){
$fname= $_POST["fname"];
$lname= $_POST["lname"];
$fullname= $fname.$lname."<br>";
echo $fullname;
}
function str_cat($fname,$lname){

9
$flname= $fname.$lname;
echo $flname;
}
string_cat();
str_cat($_POST["fname"],$_POST["lname"]);

?>

8.write a php program to display information of php in the browser.

<?php
echo "php version=".phpversion();
Phpinfo();

10
phpinfo(INFO_MODULES);
?>

9.Write a program in PHP to sort the array of given 5 numbers in ascending


and descending order.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array Sorting</title>
</head>
<body>
<h2>Array Sorting</h2>

<?php
// Given array of numbers
$numbers = array(5, 3, 9, 1, 7);
$count = count($numbers);

echo "<p>Original array:</p>";


for ($i = 0; $i < $count; $i++) {
echo "<p>arr[$i] = $numbers[$i]</p>";
}
echo "<br>";

11
// Sort the array in ascending order
for ($i = 0; $i < $count - 1; $i++) {
for ($j = 0; $j < $count - $i - 1; $j++) {
if ($numbers[$j] > $numbers[$j + 1]) {
// Swap elements if they are in the wrong order
$temp = $numbers[$j];
$numbers[$j] = $numbers[$j + 1];
$numbers[$j + 1] = $temp;
}
}
}

echo "<p>Sorted array in ascending order:</p>";


for ($i = 0; $i < $count; $i++) {
echo "<p>arr[$i] = $numbers[$i]</p>";
}
echo "<br>";

// Sort the array in descending order


for ($i = 0; $i < $count - 1; $i++) {
for ($j = 0; $j < $count - $i - 1; $j++) {
if ($numbers[$j] < $numbers[$j + 1]) {
// Swap elements if they are in the wrong order
$temp = $numbers[$j];
$numbers[$j] = $numbers[$j + 1];
$numbers[$j + 1] = $temp;
}
}
}

echo "<p>Sorted array in descending order:</p>";


for ($i = 0; $i < $count; $i++) {
echo "<p>arr[$i] = $numbers[$i]</p>";
}
?>
</body>
</html>

12
10.Write a program to count the total number of times a specific values
appears in an array.

<?php
// Given array
$array = array(1, 2, 3, 4, 2, 5, 2, 6, 2);

// Value to count occurrences of in array

13
$value = 2;

// Initialize count variable for array


$countArray = 0;

// Loop through the array and count occurrences of the specified value
foreach ($array as $element) {
if ($element == $value) {
$countArray++;
}
}

// Output the result for array


echo "The value $value appears $countArray times in the array.<br>";

$string = "The cat sat on the mat. The cat sat on the mat. The cat sat on the
mat.";

// Word to count occurrences of in string


$word = "cat";

// Initialize count variable for string


$countString = 0;

// Count occurrences of the specified word in the string


$pos = strpos($string, $word);
while ($pos !== false) {
$countString++;
$pos = strpos($string, $word, $pos + strlen($word));
}

// Output the result for string


echo "The word '$word' appears $countString times in the string: '$string'.";

?>

14
UNIT-2

1.create a form containing two input fields(name,


email_id)and a submit button. when the user clicks
on submit button ,the form data should be sent for
processing to php file, which should display the
welcome message with the email_id on the php page.
form data should be sent by http GET/POST method.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>book u2 1</title>
</head>
<body>
<form action="submit_book1_2.php" method="post">
<table>
<tr>
<td><label for="name">NAME</label></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><label for="email">EMAIL</label></td>
<td><input type="email" name="email"></td>
</tr>
<tr>

<td><input type="submit" name="submit"></td>


</tr>
</table>
</form>
</body>
</html>

15
*.Submit_book1_2.php
<?php
$name=$_POST["name"];
$email=$_POST["email"];
echo "form submitted<br>";
echo "fullname=".$name."<br>";
echo "email=".$email."<br>";
?>

2.write a php script for that creates a database


named “DB-1” in MYSQL.
<?php
$host = "localhost";
$user = "root";
$password = "";
$port = "4306";
$conn = mysqli_connect($host, $user, $password, "",$port);
if ($conn) {

16
echo "Connection successful<br>";
} else {
echo "Connection refused: " . mysqli_connect_error();
}

$db_create = "CREATE DATABASE IF NOT EXISTS db1";


$qry = mysqli_query($conn, $db_create);
if ($qry) {
echo "Database created successfully";
} else {
echo "Error creating database:<br> " . mysqli_error($conn);
}
?>

17
3.write a php script for creating a product table
in the specified database with fields prod-
id,prod_name,prod_name,prod_price,QOH. Also display
an acknowledgement for the same.

<?php
$host="localhost";
$user="root";
$password="";
$database="db1";
$port="4306";
$conn=mysqli_connect($host,$user,$password,$database,$port);
if($conn){
echo "connected<br>";
}
else{
echo "not connected<br>";
}
$db_create="CREATE TABLE product
(
prod_id INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
prod_name VARCHAR(100) NOT NULL,
prod_price FLOAT(7,2) NOT NULL,
QOH INT(5) NOT NULL
)";

$query=mysqli_query($conn,$db_create);

if($query){
echo "product table created successfully<br>";
}
else{
echo "product table could not be created <br>";
}
?>

18
4.create a form containing four input
fields(prod_id,prod_name,prod_price,QOH) and submit
button. When the user clicks on the submit button
an php script should be executed which inserts the
record in the product table.
<?php
if(isset($_GET["msg"]))
{
echo $_GET["msg"];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Product</title>

19
</head>
<body>
<form action="product_record_book4_2.php" method="post">
<table>
<caption>Add Product</caption>
<tr>
<td><label for="name">Name</label></td>
<td><input type="text" name="prod_name"></td>
</tr>
<tr>
<td><label for="prod-price">Price</label></td>
<td><input type="number" name="prod_price"></td>
</tr>
<tr>
<td><label for="QOH">Quantity</label></td>
<td><input type="number" name="QOH"></td>
</tr>
<tr>
<td><input type="submit" name="submit"></td>
<td><input type="reset" name="reset"></td>
</tr>
</table>
</form>
</body>
</html>

*.Product_record_book4_2.php:
<?php
include("book4_2.php");

$conn = mysqli_connect("localhost", "root", "", "db1", 4306);


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());

20
}

if(isset($_POST["submit"])) {
$productname = $_POST["prod_name"];
$productprice = $_POST["prod_price"];
$qoh = $_POST["QOH"];

if(!empty($productname) && !empty($productprice) && !empty($qoh)){


$query = "INSERT INTO product (prod_name, prod_price, QOH) VALUES
('$productname', '$productprice', '$qoh')";
$res = mysqli_query($conn, $query);

if($res){
header("location: product_record_book4_2.php");
exit;
} else {
header("location: book4_2.php?msg=Please try again");
exit;
}
} else {
echo "One or more fields are not set!";
}
}

$sel_query = "SELECT * FROM product";


$res = mysqli_query($conn, $sel_query);

if (!$res) {
die("Query failed: " . mysqli_error($conn));
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Records</title>
</head>
<body>
<h3><a href="book4_2.php">+ Add New Record</a></h3>
<table>
<caption>Product Records</caption>
<tr>
<th>Product id</th>
<th>Product name</th>
<th>Product price</th>
<th>Product quantity</th>

21
</tr>

<?php
while($row = mysqli_fetch_array($res)) {
?>
<tr>
<td><?php echo $row["prod_id"]; ?></td>
<td><?php echo $row["prod_name"]; ?></td>
<td><?php echo $row["prod_price"]; ?></td>
<td><?php echo $row["QOH"]; ?></td>

</tr>
<?php
}
?>
</table>
</body>
</html>

<?php

mysqli_close($conn);
?>

5.create a form containing one input


field(prod_id) and a search button. When the user
clicks on the search button a php script should get
executed and should display the details of the
product for the prod_id specified.
<?php

22
$host = "localhost";
$user = "root";
$password = "";
$database = "db1";
$port = "4306";
$conn = mysqli_connect($host, $user, $password, $database, $port);

if (!empty($_POST["search_product"])) {
$search = $_POST["search_product"];
$sel_query = "SELECT * FROM product WHERE prod_id = $search";
$res = mysqli_query($conn, $sel_query);
} else {
$sel_query = "SELECT * FROM product";
$res = mysqli_query($conn, $sel_query);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div style="float:left;width:150px;"><h3><a href="book4_2.php">+new
record</a></h3></div>
<div style="float:right;width:150px;margin-right:100px;">
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
<td>
<select name="search_product">
<option value="">Please select</option>
<?php
$res_search = mysqli_query($conn, "SELECT * FROM
product");
while ($row_search =
mysqli_fetch_array($res_search)) {
?>
<option value="<?php echo
$row_search["prod_id"]; ?>">
<?php echo $row_search["prod_name"]; ?>
</option>
<?php
}
?>
</select>
</td>

23
<td>
<input type="submit" name="submit" value="Search">
</td>
</tr>
</table>
</form>
</div>
<div style="float:left;margin-top:50px;">
<table border="1">
<caption>Product Records</caption>
<tr>
<th>Product id</th>
<th>Product name</th>
<th>Product price</th>
<th>Product quantity</th>
</tr>
<?php
while($row = mysqli_fetch_array($res)) {
?>
<tr>
<td><?php echo $row["prod_id"]; ?></td>
<td><?php echo $row["prod_name"]; ?></td>
<td><?php echo $row["prod_price"]; ?></td>
<td><?php echo $row["QOH"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>

24
6.create a form containing two input fields
(prod_id,QOH) and update button.when user clicks on
the update button the quanitity of the prod_id
specified should get updated using a php script.

<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "db1";
$port = "4306";
$conn = mysqli_connect($host, $user, $password, $database, $port);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Initialize variables
$pid = $qty = $msg = '';

// If form is submitted, fetch product details


if (isset($_POST["prod_id"])) {
$prod_id = $_POST["prod_id"];
$sel_product = "SELECT * FROM product WHERE prod_id=$prod_id";
$res_product = mysqli_query($conn, $sel_product);

if ($res_product && mysqli_num_rows($res_product) > 0) {


$row_product = mysqli_fetch_assoc($res_product);

25
$pid = $row_product["prod_id"];
$qty = $row_product["QOH"];
} else {
$msg = "Error: Product not found.";
}
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Update</title>
</head>
<body>
<form action="product_update.php" method="post">
<table>
<caption>Product Update</caption>
<tr>
<td><label for="product">Product name</label></td>
<td>
<select name="prod_id">
<option value="">Please select</option>

<?php
$sel_products = "SELECT * FROM product ORDER BY
prod_name ASC";
$qry = mysqli_query($conn, $sel_products);
while($row_search = mysqli_fetch_array($qry)) {
?>
<option value="<?php echo
$row_search["prod_id"];?>" <?php echo ($row_search["prod_id"] == $pid) ?
"selected" : ""; ?>>
<?php echo $row_search["prod_name"];?>
</option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<td><label for="quantity">Quantity</label></td>
<td><input type="number" name="QOH" value="<?php echo
$qty;?>"></td>
</tr>
<tr>
<td colspan="2">

26
<input type="submit" name="submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</form>

<?php if ($msg): ?>


<p><?php echo $msg; ?></p>
<?php endif; ?>

</body>
</html>

27
7.create a form containing one input field(prod_id)
and a delete button. When the user clicks on the
delete button a php script should get executed aand
should delete the record of the product for prod_id
specified.
Book7_2.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Delete Product</title>
</head>
<body>
<h2>Delete Product</h2>
<form action="delete_product.php" method="post">
<label for="prod_id">Product ID:</label>
<input type="text" id="prod_id" name="prod_id">
<button type="submit">Delete</button>
</form>
</body>
</html>

Product_record_book7_2.php
<?php

$conn = mysqli_connect("localhost", "root", "", "db1", 4306);


if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sel_query = "SELECT * FROM product";


$res = mysqli_query($conn, $sel_query);

if (!$res) {
die("Query failed: " . mysqli_error($conn));
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

28
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Records</title>
</head>
<body>
<table border="1">
<caption>Product Records</caption>
<tr>
<th>Product id</th>
<th>Product name</th>
<th>Product price</th>
<th>Product quantity</th>

</tr>

<?php
while($row = mysqli_fetch_array($res)) {
?>
<tr>
<td><?php echo $row["prod_id"]; ?></td>
<td><?php echo $row["prod_name"]; ?></td>
<td><?php echo $row["prod_price"]; ?></td>
<td><?php echo $row["QOH"]; ?></td>

</tr>
<?php
}
?>
</table>
</body>
</html>

<?php
mysqli_close($conn);
?>

*.Delete_product.php

<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if prod_id is provided
if (isset($_POST["prod_id"]) && !empty(trim($_POST["prod_id"]))) {
// Include your database connection code here
include("book7_2.php");

$conn = mysqli_connect("localhost", "root", "", "db1", 4306);

29
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// Prepare a SQL statement to delete the record


$sql = "DELETE FROM product WHERE prod_id = ?";

if ($stmt = $conn->prepare($sql)) {
// Bind variables to the prepared statement as parameters
$stmt->bind_param("i", $param_prod_id);

// Set parameters
$param_prod_id = $_POST["prod_id"];

// Attempt to execute the prepared statement


if ($stmt->execute()) {
echo "Product deleted successfully.";
} else {
echo "Error deleting product.";
}

// Close statement
$stmt->close();
}

// Close connection
$conn->close();

// Display the table after deletion


include("product_record_book7_2.php");
} else {
echo "Please provide a product ID.";
}
}

?>

30
31

You might also like