I will explain this thing with the help of some source code. So that you can use the same technique used in my source code in your case.
Please note that the database, table, fieldnames used in my source code might be different from your source code.
The first php page will extract the database records and display them into the page. Along with the table fields there will be another field which will be an hyperlink based on an image
PHP Code:
<?php
class database
{
private $db_handle;
private $user_name;
private $password;
private $data_base;
private $host_name;
private $sql;
private $results;
function __construct($host="localhost",$user,$passwd)
{
$this->db_handle = mysql_connect($host,$user,$passwd);
}
function dbSelect($db)
{
$this->data_base = $db;
if(!mysql_select_db($this->data_base, $this->db_handle))
{
error_log(mysql_error(), 3, "/phplog.err");
die("Error connecting to Database");
}
}
function executeSql($sql_stmt)
{
$this->sql = $sql_stmt;
$this->result = mysql_query($this->sql);
}
function returnResults()
{
return $this->result;
}
}
//$host = "jp";
$user = "root";
$passwd = "";
$db = "newTest";
$sql = "SELECT * FROM EMP ORDER BY empname";
$dbObject = new database($host,$user,$passwd);
$dbObject->dbSelect($db);
$dbObject->executeSql($sql);
//$dbObject->outputGenerate();
$res = $dbObject->returnResults();
echo "<table cellspacing='0' cellpadding='0' width='600' border='1'>\n<tr>";
echo "<td width='100'>Employee ID</td>";
echo "<td width='100'>Employee Name</td>";
echo "<td width='100'>Employee Sal</td>";
echo "<td width='100'>Employee Age</td>";
echo "<td width='100'>Employee Comm</td>";
echo "<td width='100'>Delete</td>";
echo "</tr><tr>";
while($record = mysql_fetch_object($res))
{
echo "<td width='100'>".$record->empid."</td>";
echo "<td width='100'>".$record->empname."</td>";
echo "<td width='100'>".$record->empsal."</td>";
echo "<td width='100'>".$record->empage."</td>";
echo "<td width='100'>".$record->empcomm."</td>";
echo "<td width='100'><a href='[U]delete.php?id=".$record->empid.[/U]"'><img src='1.jpg' alt='delete' border='0'></a></td>";
echo "</tr><tr><td colspan='5'> </td></tr><tr>";
}
echo "</tr></table>";
?>
Code:
echo "<td width='100'><a href='delete.php?id=".$record->empid."'><img src='1.jpg' alt='delete' border='0'></a></td>";
In the abov line which displays the delete button in the above source code is in which 1.jpg is the cross image which meant for delete.
If you look at the above mentioned line (red color)
Code:
href='delete.php?id=".$record->empid."'
It actually refers another file (delete.php) and passes a querystring parameter into it which is id which has the value of empid (which is using as the unique identifier to identify my records). In your case you can pass your unique identifier, If you pass a name there is a chance of deleting more than one records because there is a chance of having multiple records with the same name.
The second source code which I am going to paste here is delete.php
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
class database
{
private $db_handle;
private $user_name;
private $password;
private $data_base;
private $host_name;
private $sql;
private $results;
function __construct($host="localhost",$user,$passwd)
{
$this->db_handle = mysql_connect($host,$user,$passwd);
}
function dbSelect($db)
{
$this->data_base = $db;
if(!mysql_select_db($this->data_base, $this->db_handle))
{
error_log(mysql_error(), 3, "/phplog.err");
die("Error connecting to Database");
}
}
function executeSql($sql_stmt)
{
$this->sql = $sql_stmt;
mysql_query($this->sql);
}
}
//$host = "jp";
$user = "root";
$passwd = "";
$db = "newTest";
$id = $_GET['id'];
$sql = "DELETE FROM EMP WHERE empid=$id";
$dbObject = new database($host,$user,$passwd);
$dbObject->dbSelect($db);
$dbObject->executeSql($sql);
echo "<script type='text/javascript'>location.href='db1.php';</script>";
?>
</body>
</html>
The above line gets the unique identifier from the first page which here is the employee id using which we are going to delete a record from the employee table.
Code:
$sql = "DELETE FROM EMP WHERE empid=$id";
The above one is the DELETE SQL query using which we delete the record of a particular employee.
Code:
echo "<script type='text/javascript'>location.href='db1.php';</script>";
This line is for redirecting from delete.php page to the database record display page (in my case it is db1.php).
If you still have any doubt about how to implement this operation plz let me know about it.
Bookmarks