PDA

View Full Version : How to remove?



kaali
02-06-2007, 07:46 AM
Hello!

How to remove the entry from database, while viewing it from web:confused:




k

codeexploiter
02-06-2007, 08:30 AM
You can view the following tutorials/examples based on that you can tackle your problem (I assume that you are using PHP & MySQL).

1. PHP-MySQL Tutorial (http://www.php-mysql-tutorial.com/)

2. MySQL Tutorial (http://www.tizag.com/mysqlTutorial/)

kaali
02-08-2007, 01:14 PM
You can view the following tutorials/examples based on that you can tackle your problem (I assume that you are using PHP & MySQL).

1. PHP-MySQL Tutorial (http://www.php-mysql-tutorial.com/)

2. MySQL Tutorial (http://www.tizag.com/mysqlTutorial/)


Hi!

Its my outpage code


<?

$host = 'localhost';
$username="";
$password="";
$database="";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM comment";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"name");
$number=mysql_result($result,$i,"number");
$comment=mysql_result($result,$i,"comment");


echo "<table cellspacing='0' cellpadding='0' width='100%' border='1'>\n<tr>";
echo "<tr><td width=\"15%\">Name</td><td width=\"15%\">Number</td><td

width=\"70%\">Comments</td></tr><tr>";

{
echo "<td width='15%'>".$name."</td>";
echo "<td width='15%'>".$number."</td>";
echo "<td width='70%'>".$comment."</td>";
echo "</tr><tr><td colspan='3'>&nbsp;</td></tr><tr>";
}
echo "</tr></table>";


$i++;
}

?>


When this page loads, it displays all records of database.
1 want to use 4th column in which there will be option to delete the single entry, just like in phpMYadmin

Preview:-
http://img441.imageshack.us/img441/7508/dbsv1.jpg

The above 2 mentioned sites didn;t helpful!

K

superjadex12
02-10-2007, 03:32 AM
you could set up the fourth column to have links like so :


<a href="delete_entry.php?name=<?=$name;?>">delete</a>

Now this assumes that every name is unique. If they are not, its better to go with the primary key/id for that field, because you want to delete ONLY that unique field.The links will pass the name id via GET to "delete_entry.php"
Your delete_entry.php will look something like this :


$name = $_GET[name];
$query = "DELETE FROM comment WHERE name=$name";
$result = mysql_query($query) or die ("Error in query: $query.".mysql_error());

The execution of delete_entry.php will delete the row from your table.
You can also just put this code on the top of the same page, then use an
if(isset($name))
to check if the $name var is set, then run that code. Then the page will continue and reload your updated table:

If you need the code, let me know, but eh , at first glance, your table looked a little confusing so I didn't try to update your code! sorry. haha
Also, in your echo statements, its better to use ' ' wherever possible.
For example for regular html entries where you don't need any parsing, its best to u ' ' rather than " " (php has to parse EVERYTHING within " " )

kaali
02-12-2007, 09:25 PM
Hi!



If you need the code, let me know, but eh , at first glance, your table looked a little confusing so I didn't try to update your code! sorry. haha
Also, in your echo statements, its better to use ' ' wherever possible.
For example for regular html entries where you don't need any parsing, its best to u ' ' rather than " " (php has to parse EVERYTHING within " " )


The database is looking confusing bc it is showing 4 columns, but the code mentioned is meant for 3 columns, Indeed the above pic! I just pasted,
to show what i want!



Can you Give me Modified code for it?

K

kaali
02-24-2007, 05:43 PM
BUZZZZZZ:confused:

codeexploiter
02-26-2007, 09:03 AM
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
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='delete.php?id=".$record->empid."'><img src='1.jpg' alt='delete' border='0'></a></td>";
echo "</tr><tr><td colspan='5'>&nbsp;</td></tr><tr>";
}
echo "</tr></table>";
?>



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)

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



<!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>



$id = $_GET['id'];

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.


$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.


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.

kaali
03-01-2007, 04:03 PM
Hi!

I'm currently out of country! Will be Back Soon!

& Test & Reply !

K