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

Form PHP Mysql

The document describes a project to create a database and tables to store item and customer information. It includes PHP code to: 1) Create a database called "supermarket" and tables to store items and customer data 2) Insert sample data into the items and customer tables 3) Provide HTML forms and PHP code to insert, update, delete and display customer records from the database tables.

Uploaded by

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

Form PHP Mysql

The document describes a project to create a database and tables to store item and customer information. It includes PHP code to: 1) Create a database called "supermarket" and tables to store items and customer data 2) Insert sample data into the items and customer tables 3) Provide HTML forms and PHP code to insert, update, delete and display customer records from the database tables.

Uploaded by

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

IP-Section B and Extension Program CoSc-3101

Project I
Item.html
<html>
<body>
<h1>Items Information</h1>
<b><form action="item.php" method="GET">
ItemId:<input type="text" name="id"><br>
ItemName:<input type="name" name="name"><br>
ItemType:<select name="type">
<option value="Computer">Computer</option>
<option value="House Hold">House Hold</option>
<option value="Others">Others</option>
</select><br>
Price:<input type="number" name="price"><br>
<input type="submit"></form></b>
</body></html>

1 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


Item.php
<html>
<body>
<?php echo "<h1>Items Information</h1>" ?><br>
<?php echo "<b>ItemId:</b>" ?> <?php echo $_GET["id"]; ?><br>
<?php echo "<b>Item Name:</b>" ?> <?php echo $_GET["name"]; ?><br>
<?php echo "<b>Item Type: </b>" ?><?php echo $_GET["type"]; ?><br>
<?php echo "<b>Price:</b>" ?> <?php echo $_GET["price"]; ?><br>
</body>
</html

Project II
2.

a.

create database supermarket;

create table items(Itemid int primary key, ItemName varchar(30),Status char, ManufacturedDate date);

Itemid ItemName Status ManufacturedDate

create table customer(Customerid int primary key,CustomerName varchar(30),Sex char,DateOfBirth


date);

Customerid CustomerName Sex DateOfBirth

2 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


insert into items values (1,"HP","Y","12/02/2003");
insert into items values (2,"Dell","Y","01/01/2000");
insert into items values (3,"Laser Printer","N","02/04/2004");
insert into items values (4,"TV","Y","11/03/2006");
insert into items values (5,"Scanner","N","01/07/2001");
insert into items values (6,"Copy Machine","Y","12/08/2003");
insert into items values (7,"Camera","N","10/02/2012");
insert into items values (8,"Mobile","N","22/05/2010");
insert into items values (9,"DVD player","Y","12/11/2008");
insert into items values (10,"DVD writer","Y","17/03/2011");
insert into items values (11,"External Harddisk","Y","24/09/2003");

Itemid ItemName Status ManufacturedDate


1 HP Y 12/02/2003
2 Dell Y 01/01/2000
3 Laser Printer N 02/04/2004
4 TV Y 11/03/2006
5 Scanner N 01/07/2001
6 Copy Machine Y 12/08/2003
7 Camera N 10/02/2012
8 Mobile N 22/05/2010
9 DVD Player Y 12/11/2008
10 DVD Writer Y 17/03/2011
11 External Harddisk Y 24/09/2003

insert into items values (1," Abebe ","M","12/02/1977");


insert into items values (2," Saron ","F","01/01/1970");
insert into items values (3," Ermias ","M","02/04/1980");
insert into items values (4," Selam ","F","11/03/1967");
insert into items values (5," Kibrom ","M","01/07/1990");
insert into items values (6," Samson ","M","12/08/1985");
insert into items values (7," Kalkidan ","F","10/02/1989");
insert into items values (8," Biruk ","M","22/05/1979");
insert into items values (9," Tsedey r","F","12/11/1974");
insert into items values (10," Henok ","M","17/03/1969");
insert into items values (11," Abel ","M","24/09/1983");

Customerid CustomerName Sex DateOfBirth


1 Abebe M 12/02/1977
2 Saron F 01/01/1970
3 Ermias M 02/04/1980
4 Selam F 11/03/1967
5 Kibrom M 01/07/1990
6 Samson M 12/08/1985
7 Kalkidan F 10/02/1989
8 Biruk M 22/05/1979
9 Tsedey F 12/11/1974

3 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


10 Henok M 17/03/1969
11 Abel M 24/09/1983

b. Save by Home.html

<html>

<body>

<br>

<table>

<tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

<td><a href="update.html">Update Record</a>|</td>

</tr>

</table>

<br>

Click on the menu given above to insert, retrieve or modify records this Official Pages for IP-
Section B Students.

</body></html>

c. Save by insert.html

html>

<body>

<br>

<table>

<tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

<td><a href="update.html">Update Record</a>|</td>

4 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


</tr>

</table>

<br>

<h1>Customers Information</h1>

<b><form action="insert.php" method="POST">

CustomerId:<input type="number" name="id"><br>

CustomerName:<input type="name" name="name"><br>

Sex:<select name="sex">

<option value="M">M</option>

<option value="F">F</option>

</select><br>

DateOfBirth:<input type="Date" name="dob"><br>

<input type="submit"></form></b>

<body></html>

d) Save by insert.php

<html>

<body>

<br>

<table><tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

<td><a href="update.html">Update Record</a>|</td></tr></table>

<?php

$con=mysqli_connect("localhost","root","meti","supermarket");

// Check connection

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: " . mysqli_connect_error();

5 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


$sql="INSERT INTO customer(Customerid,CustomerName,Sex,DateOfBirth)

VALUES

('$_POST[id]','$_POST[name]','$_POST[sex]','$_POST[dob]')";

if (!mysqli_query($con,$sql))

die('Error: ' . mysqli_error($con));

?>

<br><b><font colour="red">1 record successfully added!</font></b>

<?php

mysqli_close($con);

?>

</body></html>

e) save by update.html

<html>

<body>

<br>

<table>

<tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

<td><a href="update.html">Update Record</a>|</td>

</tr>

</table>

<br>

Enter the Id of the customer you want to update.

<b><form action="open.php" method="POST">

CustomerId:<input type="number" name="id"><br>

6 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


<input type="submit" Value="Open"></form></b>

<body></html>

f) save by update.php

<html>

<body>

<br>

<table><tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

<td><a href="update.html">Update Record</a>|</td></tr></table>

<?php

$con=mysqli_connect("localhost","root","meti","supermarket");

// Check connection

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: " . mysqli_connect_error();

$sql="update customer set


CustomerName='$_POST[name]',Sex='$_POST[sex]',DateOfBirth='$_POST[dob]' where
Customerid='$_POST[id]'";

if (!mysqli_query($con,$sql))

die('Error: ' . mysqli_error($con));

?>

<br><b><font colour="red">1 record successfully updated!</font></b>

<?php

mysqli_close($con);

7 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


?>

</body></html>

g) save by delete.html

<html>

<body>

<br>

<table>

<tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

<td><a href="update.html">Update Record</a>|</td>

</tr>

</table>

<br>

Enter the Id of the customer you want to delete from the table.

<b><form action="delete.php" method="POST">

CustomerId:<input type="number" name="id"><br>

<input type="submit" Value="Delete"></form></b>

<body></html>

h) save by delete.php

<html>

<body>

<br>

<table>

<tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

8 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


<td><a href="update.html">Update Record</a>|</td>

</tr>

</table>

<br>

<?php

$inp=$_POST[id];

$con=mysqli_connect("localhost","root","","supermarket");

// Check connection

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: " . mysqli_connect_error();

$result = mysqli_query($con,"select * from customer");

$rec=0;

$f=0;

while($row = mysqli_fetch_array($result))

$rec=$row['Customerid'];

if($inp==$rec)

mysqli_query($con,"delete from customer where Customerid='$_POST[id]'");

echo "<b>";

echo "1 record successfully Deleted!";

echo "</b>";

$f+=1;

if($f==0)

9 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


{

echo "<b>";

echo "Record not available!";

echo "</b>";

mysqli_close($con);

?>

<body></html>

i)save by display.html
<html>

<body>

<br>

<table>

<tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

<td><a href="update.html">Update Record</a>|</td>

</tr>

</table>

<br>

<h1>Customers Information</h1>

<?php

$con=mysqli_connect("localhost","root","meti","supermarket");

// Check connection

if (mysqli_connect_errno())

10 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

$result = mysqli_query($con,"select * from customer");

echo "<b><table border=1>

<tr><td>Customerid</td><td>CustomerName</td><td>Sex</td><td>DateOfBirth</td></tr></
b>";

while($row = mysqli_fetch_array($result))

echo "<tr>";

echo "<td>" . $row['Customerid'] . "</td>";

echo "<td>" . $row['CustomerName'] . "</td>";

echo "<td>" . $row['Sex'] . "</td>";

echo "<td>" . $row['DateOfBirth'] . "</td></tr>";

echo "</table>";

mysqli_close($con);

?>

</table>

</body></html>

11 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


j) save by open.php
<html>

<body>

<br>

<table>

<tr>

<td>|<a href="insert.html">Insert New Record</a>|</td>

<td><a href="display.php">Display Records</a>|</td>

<td><a href="delete.html">Delete Record</a>|</td>

<td><a href="update.html">Update Record</a>|</td>

</tr>

</table>

<br>

<h1>Customer Information</h1>

<?php

$id=$_POST[id];

$con=mysqli_connect("localhost","root","meti","supermarket");

// Check connection

if (mysqli_connect_errno())

echo "Failed to connect to MySQL: " . mysqli_connect_error();

$result = mysqli_query($con,"select * from customer");

echo "<b><Form action='update.php' method='POST'>";

12 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


while($row = mysqli_fetch_array($result))

if($id==$row['Customerid'])

echo "Customerid:<input type='number' name='id' value='" . $row['Customerid'] .


"'><br>";

echo "CustomerName:<input type='text' name='name' value='" .


$row['CustomerName'] . "'><br>";

echo "Sex:<input type='text' name='sex' value='" . $row['Sex'] . "'><br>";

echo "DateOfBirth:<input type='Date' name='dob' value='" . $row['DateOfBirth'] .


"'><br>";

echo "<input type='submit' value='Update'>";

echo "</form>";

$f+=1;

if($f==0)

echo "<br>Record Not found!";

mysqli_close($con);

?>

</table>

</body></html>

13 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


Project III
Database Connection
1. Create a database called registrar.(createdb.php)

<?php
$connection=mysqli_connect("localhost","root","");
if($connection){
$sql="create database registrar";
if(mysqli_query($connection,$sql))
echo "Database Created Successfully";
else
die("Database not created:".mysqli_error($connection));
mysqli_close($connection);
}else
die("connection failed:".mysqli_error($connection));
?>
2. Create a table called course that contains the following attributes under registrar database.
(createtable.php)

Column Name Data type and size Constraint


CCode Varchar(10) Primary key
CTitle Varchar(30)
CrHr integer

<?php
$connection=mysqli_connect("localhost","root","");
if($connection){
if(mysqli_select_db($connection,"registrar")){
$sql="create table course(
CCode varchar(10) primary key,
CTitle varchar(25),
CrHr integer)";
if(mysqli_query($connection,$sql))
echo "Table Created Successfully";
else
die("Table not created:".mysqli_error($connection));
}
mysqli_close($connection);
}else
die("connection failed:".mysqli_error($connection));

3. Create the following form and write php scripts that allow users to insert course information
into course table though the form.(registercourse.php)
14 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet
<html><body>
<h1>Course Registration Form</h1><hr>
<?php
if(isset($_POST["register"])){
$ccode=$_POST["ccode"];
$ctitle=$_POST["ctitle"];
$crhr=$_POST["crhr"];
$connection=mysqli_connect("localhost","root","","registrar");
if($connection)
{
$sql="Insert into course(CCode,CTitle,CrHr)values('$ccode','$ctitle','$crhr')";
if(mysqli_query($connection,$sql))
echo "<b><i><font color=red>Record inserted successfully</font></i></b>";
else
die("Record not inserted:".mysqli_error($connection));
mysqli_close($connection);
}else
die("connection failed:".mysqli_error($connection));
}
?>
<form action="" method="post">
<table><tr><td>Course Code:<input type="text" name="ccode" required></td></tr>
<tr><td>Course Title:<input type="text" name="ctitle" required></td></tr>
<tr><td>Credit Hour:<input type="number" name="crhr" required></td></tr>
<tr><td><input type="submit" value="Register" name="register">&nbsp;<input type="reset" name="reset"
value="Reset"></td></tr></table>
</form><hr></body></html>

4. Write a php script that displays all the registered courses from course table in the following
format.(viewcourse.php)

Course Code Course Title Credit Hour


CoSc3101 Internet Programming II 3
CoSc3202 Wireless Networking 3
CoSc3303 Network and System 3
Administration

15 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


<html><body><?php
$connection=mysqli_connect("localhost","root","","registrar");
if($connection){
$sql="select * from course";
$result=mysqli_query($connection,$sql);
if($result){
echo "<table border=1><tr><th>Course Code</th><th>Course Title</th><th>Credit
Hour</th></tr>";
while($rows=mysqli_fetch_assoc($result)) {
echo "<tr><td>".$rows["CCode"]."</td><td>".$rows["CTitle"]."</td>
<td>".$rows["CrHr"]."</td></tr>";
}
echo
5. "</table>";
Search a course by course code and update/delete its detail.(updatecourse.php)
}else
die("unable to retrieve the records:".mysqli_error($connection));
mysqli_close($connection);
}else
die("connection failed:".mysqli_error($connection));
?>
</body></html>

16 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet


<html><body><form action="" method="post">
<table width=300><tr><td><fieldset><legend>Search Course</legend>
<input type="text" name="coursecode" placeholder="Course Code" required><input type="submit" name="search"
value="Search"></fieldset></td></tr></table></form>
<?php
$con=mysqli_connect("localhost","root","","registrar");
if($con)
{
if(isset($_POST["search"]))
{
$coursecode=$_POST["coursecode"];
$sql="select * from course where CCode='$coursecode'";
$result=mysqli_query($con,$sql);
while($row=mysqli_fetch_assoc($result))
{
echo "<form action='' method=post>Course Code “;
echo “<input type=text name=ccode value='".$row["CCode"]."'required><br>";
echo "Course Title <input type=text name=ctitle value='".$row["CTitle"]."'required><br>";
echo "Credit Hour <input type=text name=crhr value='".$row["CrHr"]."'required><br>";
echo "<input type=submit name=save value=Save>&nbsp;”;
echo “<input type=submit name=delete value=Delete></form>";
}

}else if(isset($_POST["save"]))
{
$ccode=$_POST["ccode"];
$ctitle=$_POST["ctitle"];
$crhr=$_POST["crhr"];
$sql="update course set CTitle='$ctitle',CrHr='$crhr' where CCode='$ccode'";
if(mysqli_query($con,$sql))
echo "Course detail updated successfully";
else
die("Record not updated:".mysqli_error($con));
}else if(isset($_POST["delete"]))
{
$ccode=$_POST["ccode"];
$sql="delete from course where CCode='$ccode'";
if(mysqli_query($con,$sql))
echo "Record deleted successfully";
else
die("Record not deleted:".mysqli_error($con));
}
}else
die("Connection Failed:".mysqli_error($con));
?></body></html>

17 Prepared By:-Samuel A. Internet Programming Form+PHP+MYsql Lab worksheet

You might also like