You are on page 1of 9

config.

php
add.php
list.php
edit.php
delete.php

<style type="text/css">
td
{
padding:5px;
border:1px solid #ccc;
}
</style>

<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("freeze_demo",$query);
?>

<html>
<body>
<?php
include('config.php');
if(isset($_POST['submit']))
{
$name=mysql_real_escape_string($_POST['name']);
$age=mysql_real_escape_string($_POST['age']);
$query1=mysql_query("insert into addd values('','$name','$age')");
echo "insert into addd values('','$name','$age')";
if($query1)
{
header("location:list.php");
}
}
?>
<fieldset style="width:300px;">
<form method="post" action="">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
<br>
<input type="submit" name="submit">
</form>

</fieldset>
</body>
</html>

<html>
<body>
<?php
include('config.php');
$query1=mysql_query("select id, name, age from addd");
echo "<table><tr><td>Name</td><td>Age</td><td></td><td></td>";
while($query2=mysql_fetch_array($query1))
{
echo "<tr><td>".$query2['name']."</td>";
echo "<td>".$query2['age']."</td>";
echo "<td><a href='edit.php?id=".$query2['id']."'>Edit</a></td>";
echo "<td><a href='delete.php?id=".$query2['id']."'>x</a></td><tr>";
}
?>
</ol>
</table>
</body>
</html>

edit.php
When edit option in list page is clicked, we will be redirected to edit.php and by using id value
(primary key) values will be fetched from database.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<html>

<body>
<?php
include('config.php');
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$age=$_POST['age'];
$query3=mysql_query("update addd set name='$name', age='$age' where id='$id'");
if($query3)
{
header('location:list.php');
}
}
$query1=mysql_query("select * from addd where id='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="">
Name:<input type="text" name="name" value="<?php echo $query2['name']; ?>" /><br />
Age:<input type="text" name="age" value="<?php echo $query2['age']; ?>" /><br /><br />
<br />
<input type="submit" name="submit" value="update" />
</form>
<?php
}
?>
</body>
</html>

delete.php
When delete (X) option is clicked in list page, particular record will be deleted by using id values
and redirected to list.php
1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<html>
<body>
<?php
include('config.php');
if(isset($_GET['id']))
{
$id=$_GET['id'];
$query1=mysql_query("delete from addd where id='$id'");
if($query1)
{
header('location:list.php');
}
}
?>
</body>
</html>

________________

now we will manipulate data in database (i.e. insert, edit, update and delete) with PHP.

To manipulate the data I have created more than 1 PHP files, So take a separate folder (I am taking
Day11) to put all those below mentioned files.
Note : In this assignment, I am taking the same database (mydb) and table (tbemp) that we was
created in Day9.
Program 38: Complete data manipulation with PHP.
File 1. connect.php : Used to define connection and select database.
We will include this file where database connection will required, so after this we dont have to write
connection code again and again, and also we can easily change our server and database (if
required) from one file only.
<?php
$con = mysql_connect(localhost,root,");
mysql_select_db(mydb,$con);
?>
File 2. menu.php : Used for navigation.
<a href=insert.php>Add</a> |
<a href=display.php>Display</a>
<hr>
File 3. insert.php : Used to save the records in database.
<?php
require_once connect.php;
$msg = ;
if(isset($_REQUEST['btnSubmit'])) {
$name = $_REQUEST['txtName'];
$salary = $_REQUEST['txtSalary'];
$city = $_REQUEST['txtCity'];
$query = insert into tbemp (name,salary,city) values ($name,'$salary,'$city);
if(mysql_query($query)){
$msg = Record Saved!;
} else {
$msg = Unable to Save!;
}
}
?>
<html>
<?php include menu.php; ?>
<body>

<form method=POST>
Name : <input type=text name=txtName/> <br/>
Salary : <input type=text name=txtSalary/> <br/>
City : <input type=text name=txtCity/> <br/>
<input type=submit name=btnSubmit value=Save/> <br/>
<?php echo $msg; ?>
</form>
</body>
</html>
File 4. display.php : Used to display the records from database.
Note : Query string is used to send id to file edit.php and delete.php to edit and delete the
records. In query string we can send only string values to the next page.
<?php
require_once connect.php;
$query = select * from tbemp;
$data = mysql_query($query);
?>
<html>
<?php include menu.php; ?>
<body>
<table border=1 cellpadding=5>
<tr>
<th>Name</th> <th>Salary</th> <th>City</th> <th colspan=2>Action</th>
</tr>
<?php while($rec = mysql_fetch_array($data)) { ?>
<tr>
<td> <?php echo $rec['name']; ?> </td>
<td> <?php echo $rec['salary']; ?> </td>
<td> <?php echo $rec['city']; ?> </td>
<td> <a href=edit.php?id=<?php echo $rec['id']; ?>>edit</a> </td>
<td> <a onClick=return confirm(Sure to delete!) href=delete.php?id=<?php echo $rec['id']; ?
>>delete</a> </td>
</tr>
<?php } ?>
</table>
</body>
</html>
File 5. edit.php : Used to edit and update the records.

<?php
require_once connect.php;
$msg = ;
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
if(isset($_REQUEST['btnSubmit'])) {
$name = $_REQUEST['txtName'];
$salary = $_REQUEST['txtSalary'];
$city = $_REQUEST['txtCity'];
$query1 = update tbemp set name=$name,salary=$salary,city=$city where id = .$id;
if(mysql_query($query1)){
$msg = Record Updated!;
} else {
$msg = Unable to Update!;
}
}
$query = select * from tbemp where id=.$id;
$data = mysql_query($query);
$rec = mysql_fetch_array($data);
?>
<html>
<body>
<form method=POST>
Name : <input type=text value=<?php echo $rec['name']; ?> name=txtName/> <br/>
Salary : <input type=text value=<?php echo $rec['salary']; ?> name=txtSalary/> <br/>
City : <input type=text value=<?php echo $rec['city']; ?> name=txtCity/> <br/>
<input type=submit name=btnSubmit value=Update/> <br/>
<?php echo $msg; ?>
</form>
</body>
</html>
File 5. delete.php : Used to delete the records.
<?php
require_once connect.php;
$msg = ;
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : 0;
$query = delete from tbemp where id=.$id;
if(mysql_query($query)) {
header(location:display.php);

} else {
echo unable to delete!;
}
?>

You might also like