Page 1 of 1

How to delete some informations in a table?

Posted: Sat Mar 21, 2009 10:18 pm
by MicroBoy
I have table that shows the informations from database "MySQL", I need to add a delete link that when I click delete, the Name, News and Date will delete from the table and database. The file that I use to read the informations from database:

Code: Select all

<html>
<body>
<?php
include ('menu/menu.php');
?>
 
<TABLE class="col_top" width="800" align="left" cellspacing="0" cellpadding="0" border="0">
  <TR>
<TD valign="left">
 
<?php
 
if (!$lidhja)
  {
  die('Nuk Mund Të Konektohet: ' . mysql_error());
  }
 
mysql_select_db("$databaza", $lidhja);
 
$result = mysql_query("SELECT * FROM lajmet ORDER BY ora DESC");
 
echo "<table border='1'>
<tr>
<th>Emri</th>
<th>Lajmi</th>
<th>Data&Ora</th>
</tr>";
 
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['emri'] . "</td>";
  echo "<td>" . $row['lajmi'] . "</td>";
  echo "<td>" . $row['ora'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
 
mysql_close($lidhja);
 
include ("menu/posht.php");
?>
 </TR>
</TABLE>
</body>
</html>

Re: How to delete some informations in a table?

Posted: Sat Mar 21, 2009 11:42 pm
by tech603
Given what you have have you could do something like this

# while($row = mysql_fetch_array($result))
# {
# echo "<tr>";
# echo "<td>" . $row['emri'] . "</td>";
# echo "<td>" . $row['lajmi'] . "</td>";
# echo "<td>" . $row['ora'] . "</td>";
# ech0 "<td><a href='yourdeletepage?subject=" . $row['emni'] . " '>Delete</a></td>";
# echo "</tr>";
# }

That would pass the value of your subject if that is your subject row to a delete page where you could run your mysql delete script. Just be sure to look for that value on the next page with something like this

On the delete page

if (isset($_GET['subject'])) {
$subject = $_GET['subject'];
}

Hope that helps

Matthew Vass
QA Anylist
mvass@hostmysite.com
http://www.hostmysite.com?utm_source=bb

Re: How to delete some informations in a table?

Posted: Sun Mar 22, 2009 11:40 am
by MicroBoy
You helped me to much. But now I have another problem that I had before.
I want that when I click Delete in that page I want to display just the message "News Deleted" not and the table. I'm using the same file for displaying the table and for deleting row. Here viewtopic.php?f=1&t=96918 php_east told me how to do that in another file but now I don't know how to do it in this file:

Code: Select all

<html>
<body>
<?php
include ('menu/menu.php');
?>
<TABLE class="col_top" width="800" align="left" cellspacing="0" cellpadding="0" border="0">
  <TR>
<TD valign="left">
 
<?php
 
if (!$lidhja)
  {
  die('Nuk Mund Të Konektohet: ' . mysql_error());
  }
 
mysql_select_db("$databaza", $lidhja);
 
$result = mysql_query("SELECT * FROM lajmet ORDER BY ora DESC");
 
echo "<table border='1'>
<tr>
<th>Emri</th>
<th>Lajmi</th>
<th>Data&Ora</th>
<th>Delete</th>
</tr>";
 
$id = $_GET['id'];
 
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['emri'] . "</td>";
  echo "<td>" . $row['lajmi'] . "</td>";
  echo "<td>" . $row['ora'] . "</td>";
  echo "<td><a href=lexo.php?id=" . $row['id'] . ">Delete</a></td>";
  echo "</tr>";
  }
echo "</table>";
 
if (isset($id)) 
{
mysql_query("DELETE FROM lajmet WHERE id='$id'");
echo "News deleted!";
}
 
mysql_close($lidhja);
 
include ("menu/posht.php");
?>
 </TR>
</TABLE>
</body>
</html>

Re: How to delete some informations in a table?

Posted: Tue Mar 24, 2009 12:28 pm
by tech603
The first thing i want to point is this
$id = $_GET['id'];
and then later this
# if (isset($id))
# {
# mysql_query("DELETE FROM lajmet WHERE id='$id'");
# echo "News deleted!";
# }
This could potentially cause issues if the url does not contain the value id. You can eliminate that problem by placing that line within the if isset condition. And also for it to display the table only when your not deleting you'd probably want to wrap that inside the same condition. So something like this.

Code: Select all

 
if (!$lidhja)
  {
  die('Nuk Mund Të Konektohet: ' . mysql_error());
  }
 
mysql_select_db("$databaza", $lidhja);
 
if (isset($_GET['id'])){
$id = $_GET['id']
mysql_query("DELETE FROM lajmet WHERE id='$id'");
echo "News deleted!";
}else {
 
$result = mysql_query("SELECT * FROM lajmet ORDER BY ora DESC");
 
echo "<table border='1'>
<tr>
<th>Emri</th>
<th>Lajmi</th>
<th>Data&Ora</th>
<th>Delete</th>
</tr>";
 
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['emri'] . "</td>";
  echo "<td>" . $row['lajmi'] . "</td>";
  echo "<td>" . $row['ora'] . "</td>";
  echo "<td><a href=lexo.php?id=" . $row['id'] . ">Delete</a></td>";
  echo "</tr>";
  }
echo "</table>";
}
 
I would highly recommend having your delete script on a separate page for better security reasons and to avoid accidental deletion. But if you wish to keep it on the page you probably would want to add some sort of confirmation before deleting and also some authentication checking to avoid bots accidentally deleting as well. Hope that helps point you in the right direction.

Matthew Vass
QA Analyst
mvass@hostmysite.com
http://www.hostmysite.com?utm_source=bb