LAB 1 Insert Update Delete Edit
LAB 1 Insert Update Delete Edit
Lab task 1
- This lab task one deals with the creation of database and table in MYSQL.
- Establishing connection between MYSQL and PHP
- Creating HTML form and inserting record from HTML form to MYSQL
- Displaying records from MYSQL to Web Browser.
- Editing Records and updating records
- Deleting records.
1|Page BKC
LAB TASK 1 PHP and MYSQL NKS
1. Connecting with database from PHP (db_connect.php)
<?php
?>
<html>
<head>
<title>Form Input Data</title>
</head>
<body>
<center>
<form method="post" action="input.php">
2|Page BKC
LAB TASK 1 PHP and MYSQL NKS
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" size="40"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="text" name="gender" size="40"></td>
</tr>
<tr>
<td>E-Learning</td>
<td><input type="text" name="elearning" size="40"></td>
</tr>
<tr>
<td>Java</td>
<td><input type="text" name="java" size="40"></td>
</tr>
<tr>
<td>Graphics</td>
<td><input type="text" name="graphics" size="40"></td>
</tr>
</table>
<input type="submit" name="submit" value="Insert">
</form>
</center>
</body>
</html>
3|Page BKC
LAB TASK 1 PHP and MYSQL NKS
//declare in the order variable
$result = mysql_query($order); //order executes
if($result)
{
echo"<br>";
echo("Record inserted sucessfully");
}
else
{
echo"<br>";
echo("Input data is fail check your connection");
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<?php
include("db_connect.php");
?>
<body>
<table border=1>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Address</th>
<th>Gender</th>
<th>Elearning</th>
<th>Java</th>
<th>Graphics</th>
</tr>
<?php
4|Page BKC
LAB TASK 1 PHP and MYSQL NKS
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['Name'];?></td>
<td><?php echo $row['Address'];?></td>
<td><?php echo $row['Gender'];?></td>
<td><?php echo $row['Elearning'];?></td>
<td><?php echo $row['Java'];?></td>
<td><?php echo $row['Graphics'];?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
Delete.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<form name="form1" method="post" action="delete.php">
<p>Enter Student Id to delete</p>
<p>Student Id
<input name="id" type="text">
</p>
<p>
<input type="submit" name="Submit" value="DELETE">
</p>
</form>
<body>
</body>
</html>
Delete.php
<?php
5|Page BKC
LAB TASK 1 PHP and MYSQL NKS
include("db_connect.php");
// Get values from form
$id=$_POST['id'];
$name=$_POST['name'];
$address=$_POST['address'];
$gender=$_POST['gender'];
$elearning=$_POST['elearning'];
$java=$_POST['java'];
$graphics=$_POST['graphics'];
$query="delete from student_info where id ='$id'";
$check=mysql_query($query);
include("displayintable.php");
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
edit_action.php
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
6|Page BKC
LAB TASK 1 PHP and MYSQL NKS
<?php
$id=$_POST['id'];
$name=$_POST['name'];
$address=$_POST['address'];
$gender=$_POST['gender'];
$elearning=$_POST['elearning'];
$java=$_POST['java'];
$graphics=$_POST['graphics'];
$con=mysql_connect("localhost","root","");
mysql_select_db("bkc");
$sql="update student_info set Name='$name', Address='$address',
Gender='$gender',Elearning='$elearning',Java='$java',Graphics='$graphics' where id=$id";
mysql_query($sql,$con) or die(mysql_error());
if(mysql_affected_rows($con)==1)
echo("record updated successfully");
mysql_close();
include("displayintable.php");
?>
</body>
</html>
Edit-record.php
<?php
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title></head>
<body>
<?php
$id=$_GET['id'];
if(!is_numeric($id))
{
header("location: update.html");//runs to the given location we have to start
ob_start tag to use header
exit;
}
$con=mysql_connect("localhost","root","");
mysql_select_db("bkc");
7|Page BKC
LAB TASK 1 PHP and MYSQL NKS
$sql="select *from student_info where id=$id";
$result=mysql_query($sql,$con)or die(mysql_error());
if(mysql_num_rows($result)==0)
{
echo("<font color=red size=2 > no record found</font>");
exit;
}
$data=mysql_fetch_assoc($result); print_r($data);
?>
8|Page BKC
LAB TASK 1 PHP and MYSQL NKS
<input type="submit" name="button" id="button" value="update" /></p>
</form>
</body>
</html>
9|Page BKC