hi,
I can displayed data from list/menu from 3 different tables in database in a table and in each row of the table I had a delete button. The problem is I did not know how to delete a single row when i click the button. Anyone can help?
This is how I selected my data into a table.

if(isset($_POST['display']))
{
$tables = array("Blog posts"=>"blog","News"=>"news", "Announcement"=>"announcement");
$orderby = array("Blog posts"=>"blog_id","News"=>"news_id", "Announcement"=>"announ_id");
$category = mysql_real_escape_string($_POST['list']);
$sql = "SELECT * FROM ".$tables[$category]." ORDER BY ".$orderby[$category]." ASC";
$result=mysql_query($sql);

while($nt=mysql_fetch_array($result)){
	
	 print ("<table width='780' border='1'>
  <tr>
    <td width='677'>"); echo "$nt[title]&nbsp; &nbsp;"; print("</td>
    <td width='100'>");  echo "$category<br>"; print("</td>
    <td width='30'><form name='input' action='display_allposts.php' method='post'>
<input type='submit' name='delete' value='Delete' />
</form> </td>
  </tr>
</table>");

}
}

This is my delete function

if(isset($_POST['delete']))
{
$category = mysql_real_escape_string($_POST['list']);
$tables = array("Blog posts"=>"blog","News"=>"news", "Announcement"=>"announcement");
$orderby = array("Blog posts"=>"blog_id","News"=>"news_id", "Announcement"=>"announ_id");
$sql = "DELETE FROM ".$tables[$category]." WHERE ".$orderby[$category]. " = $id";
$result=mysql_query($sql);
}

remove $result= off $result=mysql_query($sql); other wise you did it perfectly
because $result is stopping mysql_query to follow through really you really never want to put a variable in front of mysql_query unless your putting it through a loop but your not so you do not want a variable in front mysql_query ok

I still could make the button delete take any action at all.It's still not working and i had no idea why.I've changed my code into this :

while($nt=mysql_fetch_array($result)){
	$id = $nt[$orderby[$category]];
	echo "$id";
	 print ("<table width='780' border='1'>
  <tr>
    <td width='677'>"); echo "$nt[title]&nbsp; &nbsp;"; print("</td>
    <td width='100'>");  echo "$category<br>"; print("</td>
    <td width='30'><form name='input' action='display_allposts.php' method='post'>
<input type='submit' name='delete' value='Delete' /></form> </td>
  </tr>
</table>");
}
}

if(isset($_POST['delete']))
{
//$category = mysql_real_escape_string($_POST['list']);
//$tables = array("Blog posts"=>"blog","News"=>"news", "Announcement"=>"announcement");
//$orderby = array("Blog posts"=>"blog_id","News"=>"news_id", "Announcement"=>"announ_id");
$sql = "DELETE FROM ".$tables[$category]." WHERE ".$orderby[$category]. " = $id";
mysql_query($sql);
$alert = "Deletion is successful.";
echo '<script type="text/javascript">alert("'.$alert.'");</script>';
}

use this you had an error you had an extra bracket in you script that should of not been there also also to make you job easier go download waterproofs PHPEdit v3.4.0 it will help you in writing you scripts and show you your errors to fix quick

while($nt=mysql_fetch_array($result)){
	$id = $nt[$orderby[$category]];
	echo "$id";
	print ("<table width='780' border='1'>
  <tr>
    <td width='677'>");
	echo "$nt[title]&nbsp; &nbsp;";
	print("</td>
    <td width='100'>");
	echo "$category<br>";
	print("</td>
    <td width='30'><form name='input' action='display_allposts.php' method='post'>
<input type='submit' name='delete' value='Delete' /></form> </td>
  </tr>
</table>");
}

if(isset($_POST['delete']))
{
	//$category = mysql_real_escape_string($_POST['list']);
	//$tables = array("Blog posts"=>"blog","News"=>"news", "Announcement"=>"announcement");
	//$orderby = array("Blog posts"=>"blog_id","News"=>"news_id", "Announcement"=>"announ_id");
	$sql = "DELETE FROM ".$tables[$category]." WHERE ".$orderby[$category]. " = $id";
	mysql_query($sql);
	$alert = "Deletion is successful.";
	echo '<script type="text/javascript">alert("'.$alert.'");</script>';
}

check your bracket

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.