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.
Program:
HTML File:
<!DOCTYPE html>
<html>
<head>
<title>registration form</title>
</head>
<body>
<form action="welcom.php" method="POST">
Name: <input type="text" name="name"><br>
Email: <input type="email" name="email"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
PHP File: (welcome.php)
<!DOCTYPE html>
9
<html>
<head>
<title></title>
</head>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your Email Address is:<?php echo $_POST["email"]; ?><br>
</body>
</html>
Output:
HTML Page:
PHP Page:
2. Write a PHP script for that creates a database named "DB-1"in
MySQL.
Program:
<?php
10
$servername="localhost";
$username="root";
$password="";
$conn= mysqli_connect($servername, $username, $password);//create connection
//check connection
if (! $conn)
{
die("Connection failed:".$conn->connect_error());
}
echo "Connected successfully<br/>";
$Sql="CREATE DATABASE my_DB_1";
if($conn->query($Sql) === TRUE)
{
echo "Database my_DB_1 created successfully";
}
else
{
echo "sorry.. ".$conn->error;
}
$conn->close();
?>
Output:
3. Write a PHP script for creating a product table in the specified
database with fields Pro_id, Pro_name, Pro_price, QOH. Also display an
acknowledgement for the same.
Program:
<?php
$servername="localhost";
11
$username="root";
$password="";
$dbname="my_DB_1";
$conn= mysqli_connect($servername, $username, $password, $dbname);//create
connection
//check connection
if ($conn->connect_error)
die("Connection failed:".$conn->connect_error());
else
echo "Connected successfully<br/>";
$sql="CREATE TABLE Products(Pro_id INT(6) UNSIGNED AUTO_INCREMENT
PRIMARY key, Pro_name VARCHAR(30) NOT NULL, Pro_price INT(6) NOT
NULL, QOH VARCHAR(30))";
if($conn->query($sql) === TRUE)
echo "Table Productsis created successfully";
else
echo "Error creating Table:".$conn->error;
12
$conn->close();
?>
Output:
4. Create a form contaning four input fields(Pro_id, Pro_name, Pro_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.
Program:
HTML File:
<!DOCTYPE html>
<html>
<head>
<title>product deatils form</title>
</head>
<body>
<form action="Product_details.php" method="POST">
<label><b>Product ID : </b> </label> <input
type="text" name="p_id"><br>
<label><b>Product Name : </b></label><input type="text" name="p_name"><br>
<label><b>Product Price : </b> </label><input type="text"
name="p_price"><br>
<label><b>Product QOH : </b> </label><input type="text" name="qoh"><br>
<input type="submit" name="submit" value="submit">
</form>
13
</body>
</html>
PHP File: (Produt_dtails.php)
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="my_DB_1";
$conn= mysqli_connect($servername, $username, $password, $dbname);//create
connection
//check connection
if ($conn->connect_error)
die("Connection failed:".$conn->connect_error());
$sql="INSERT INTO Products(Pro_id, Pro_name, Pro_price, QOH)
VALUES('$_POST[p_id]' , '$_POST[p_name]' , '$_POST[p_price]' ,
'$_POST[qoh]')";
if($conn->query($sql) === TRUE)
echo "<b>1 Record Added Successfully:</b>";
else
14
echo "<b>Error adding record</b>";
$conn->close();
?>
Output:
HTML Page:
PHP Page:
5. Create a form contaning one input field(Pro_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 Pro_id
specified.
Program:
HTML File:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
15
<form action="search_product.php" method="POST">
<label><b>Product ID : </b></label> <input type="text" name="p_id"><br>
<input type="submit" name="search" value="search">
</form>
</body>
</html>
PHP File:
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="my_DB_1";
$conn= mysqli_connect($servername, $username, $password, $dbname);//create
connection
//check connection
if ($conn->connect_error)
die("Connection failed:".$conn->connect_error());
$sql="SELECT * FROM Products WHERE Pro_id='$_POST[p_id]'";
$result=$conn->query($sql);
if ($result->num_rows > 0)
16
//output data of each row
while ($row = $result->fetch_assoc())
echo "Prouct Id:". $row["Pro_id"]."<br>". "Prouct Name:".
$row["Pro_name"]."<br>". "Prouct Price:". $row["Pro_price"]."<br>". "Prouct
QOH:". $row["QOH"]. "<br>";
else
echo "0 results:";
$conn->close();
?>
Output:
HTML Page:
PHP Page:
17
6. Create a form contaning two input fields (Pro_id, QOH) and Update
button. When the user clicks on the Update button the quantity of the Pro_id
specified should get updated using a PHP script.
Program:
HTML File:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="update_product.php" method="POST">
<label><b>Product ID : </b></label> <input type="text" name="p_id"><br>
<label><b>Product QOH : </b></label> <input type="text" name="p_qoh"><br>
<input type="submit" name="Update" value="Update">
</form>
</body>
</html>
PHP File:
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="my_DB_1";
18
$conn= mysqli_connect($servername, $username, $password, $dbname);//create
connection
//check connection
if ($conn->connect_error)
die("Connection failed:".$conn->connect_error());
/*else
echo"Connected successfully..";*/
$sql="UPDATE Products SET QOH='$_POST[p_qoh]' WHERE
Pro_id='$_POST[p_id]'"; //query for update data
$result=$conn->query($sql);
if ($result===TRUE)
echo "<b>Data updated successfully..</b><br>";
$sql1="SELECT * FROM Products WHERE Pro_id='$_POST[p_id]'";//query for
display updated data
$results=$conn->query($sql1);
while ($row = $results->fetch_assoc())
echo "Prouct Id:". $row["Pro_id"]."<br>". "Prouct Name:".
$row["Pro_name"]."<br>". "Prouct Price:". $row["Pro_price"]."<br>". "Prouct
QOH:". $row["QOH"]. "<br>";
19
}
$conn->close();
?>
Output:
HTML Page:
PHP Page:
7. Create a form contaning one input field(Pro_id) and a Delete button.
When the user clicks on the Delete button a PHP script should get executed
and should delete the record of the product for the Pro_id specified.
Program:
HTML File:
<!DOCTYPE html>
<html>
<head>
20
<title></title>
</head>
<body>
<form action="delete_product.php" method="POST">
<label><b>Product ID : </b></label> <input
type="text" name="p_id"><br>
<input type="submit" name="Delete" value="Delete">
</form>
</body>
</html>
PHP File:
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="my_DB_1";
$conn= mysqli_connect($servername, $username, $password, $dbname);//create
connection
//check connection
if ($conn->connect_error)
die("Connection failed:".$conn->connect_error());
$sql="DELETE FROM Products WHERE Pro_id='$_POST[p_id]'";
21
if($conn->query($sql) === TRUE)
echo "<b>1 Record deleted Successfully:</b>";
else
echo "<b>Error deleting record</b>";
$conn->close();
?>
Output:
HTML Page:
PHP Page:
22