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

LAB 1 Insert Update Delete Edit

This document discusses creating a database and table in MySQL, connecting PHP to MySQL, creating an HTML form to insert records into MySQL from PHP, and displaying, editing, and deleting records from MySQL in PHP. It provides code examples to implement each of these tasks.

Uploaded by

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

LAB 1 Insert Update Delete Edit

This document discusses creating a database and table in MySQL, connecting PHP to MySQL, creating an HTML form to insert records into MySQL from PHP, and displaying, editing, and deleting records from MySQL in PHP. It provides code examples to implement each of these tasks.

Uploaded by

Dipen Dhakal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB TASK 1 PHP and MYSQL NKS

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.

Creating database in MYSQL

Steps to create database


1. Start wamp server
2. Choose phpmyadmin
3. Write name of database in colored section
Creating Table

Steps to create table


Table can be created in MYSQL in different ways
1. Using query
2. Using design view

1|Page BKC
LAB TASK 1 PHP and MYSQL NKS
1. Connecting with database from PHP (db_connect.php)
<?php

$db=mysql_connect('localhost','root','') or die('unable to connect to database');


$conn=mysql_select_db('bkc',$db) or die(mysql_error($db));
if($conn)
{
echo'database connected';
}

?>

2. Creating form to input record(html_form.html)

<html>
<head>
<title>Form Input Data</title>
</head>
<body>
<center>
<form method="post" action="input.php">

<table width="411" border="1">


<caption>
Insert Data
</caption>
<tr>
<td width="155">ID: </td>
<td width="240"><input type="text" name="id" size="40"></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="40"></td>

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. Codes to insert records from HTML to MYSQL(input.php)


<?php

//the example of inserting data with variable from HTML form


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'];
//inserting data order
$order = "INSERT INTO student_info
(id,Name,Address,Gender,Elearning,Java,Graphics)
VALUES
('$id','$name','$address','$gender','$elearning','$java','$graphics')";

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");
}

?>

4. Display Records form MYSQL to Web page (displayintable.php)

<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

//create the query


$result = mysql_query("select * from student_info");

//return the array and loop through each row


while ($row = mysql_fetch_array($result))

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");
?>

Editing Records in database


Edit.html

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<form id="form1" name="form1" method="get" action="edit-record.php">


<p>Enter roll no to edit</p>
<p>
<input type="text" name="id" id="id" />
<input type="submit" name="button" id="button" value="Submit" /></p>
<p>&nbsp;</p>
</form>
</body>
</html>

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);
?>

<form id="form1" name="form1" method="post" action="edit_action.php">

<table width="411" border="1">


<caption>
Edit Record
</caption>
<tr>
<td width="155">ID: </td>
<td width="240"><input type="text" name="id" size="40" value="<?=$data['id']?>"></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="40" value="<?=$data['Name']?>"></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" size="40" value="<?=$data['Address']?>"></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="text" name="gender" size="40" value="<?=$data['Gender']?>"></td>
</tr>
<tr>
<td>E-Learning</td>
<td><input type="text" name="elearning" size="40" value="<?=$data['Elearning']?>"></td>
</tr>
<tr>
<td>Java</td>
<td><input type="text" name="java" size="40" value="<?=$data['Java']?>"></td>
</tr>
<tr>
<td>Graphics</td>
<td><input type="text" name="graphics" size="40" value="<?=$data['Graphics']?>"></td>
</tr>
</table>

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

You might also like