Delete (row in mysql) command not working
Posted: Wed Jul 08, 2009 9:56 am
So I had been looking around on the internet for a good delete script (which I found) which will delete rows from a database table with a javascript confirmation first. Well I technically got it to work but it doesn't work and was wondering if anybody could help me out.
header.php
index.php
delnews.js
Mind you the coding is a work-in-progress and there's a lot that needs to be done to fix it up. But anyways I was trying to get the delete script to delete a table in the page (table will be deleted when it's ID is removed from the database). The javascript confirmations works and it successfully get's the id and table name to be deleted. When I press OK it loads the page as index.php?delnews=Array->14 (14 being the tables ID number) and I get an error.
So I figure OK I'll delete that line. I reload the index.php page (noticing the the table hadn't even been deleted). Go into Dreamweaver and delete the the content on line 36 which was:
And try again. I click on the delete button, I get the confirmation, I click OK and it reloads the page as index.php?delnews=Array->14 as per normal. Only problem is is that absolutely nothing (except the header) is showing in the webpage. I reload the index.php page and once again noticed the table hadn't been removed.
So can somebody please tell me what I've done wrong? How do I get it to reload the page once I've clicked the delete button and how do I get it to show all the content on the page? And why isn't it deleting the table from the database?
header.php
Code: Select all
<?php
$insight_name = "Insight";
$insight_slogan = "Racing";
$theme = "insight";
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
. "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
. "<link href=\"index.css\" rel=\"stylesheet\" type=\"text/css\" />\n"
. "<head>\n"
. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
. "<title>" . $insight_name . " :: " . $insight_slogan . "</title>\n"
. "<script language=\"javascript\" type=\"text/javascript\" src=\"../js/collapsibletable.js\"></script>\n"
. "<script language=\"javascript\" type=\"text/javascript\" src=\"../js/delnews.js\"></script>\n"
. "<link id=\"text-css\" href=\"themes/" . $theme . "/style.css\" rel=\"stylesheet\" rev=\"stylesheet\" type=\"text/css\" media=\"screen\" />\n"
. "</head>\n\n"
. "<body onload=\"init()\">\n\n"
. "<a name=\"top\"></a>\n\n";
?>Code: Select all
<?php
include("includes/constants.php");
$sql=mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die ("I cannot connect to the database because: " . mysql_error());
mysql_select_db(DB_NAME, $sql) or die ("I cannot select the database '$dbname' because: " . mysql_error());
$news = mysql_query("SELECT * FROM insight_news ORDER BY nid DESC") or die ('Error : ' . mysql_error());
while($fetchnews = mysql_fetch_array($news)) {
$id = $fetchnews['nid'];
$title = $fetchnews['title'];
$author = $fetchnews['author'];
$authorid = $fetchnews['author_id'];
$text = $fetchnews['text'];
$month = $fetchnews['month'];
$day = $fetchnews['day'];
$year = $fetchnews['year'];
$bbcodeoff = $fetchnews['bbcodeoff'];
$smileyoff = $fetchnews['smileyoff'];
echo "<div style=\"padding-bottom:15px\">\n"
. "<table cellspacing=\"0\" width=\"100%\">\n";
include("themes/insight/colors.php");
echo "<thead><tr bgcolor=\"" . $bgcolor1 . "\"><td class=\"tnews\"><span style=\"float:right\">";
if(isset($_GET['delnews']))
{
$query = mysql_query("DELETE FROM insight_news WHERE nid = '{$_GET['delnews']}'")or die('Error : ' . mysql_error());
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit;
}
echo "<a href=\"javascript:delnews('$fetchnews->$id','$fetchnews->$title')\"><img border=\"0\" src=\"themes/insight/images/delete.png\" alt=\"Delete\" /> </a>";
echo "<a href=\"#top\" onclick=\"toggleItem('" . $title . "_" . $id . "')\"><img border=\"0\" src=\"themes/insight/images/collapseobj.png\" alt=\"toggle\" /></a></span><img src=\"themes/insight/images/ticon.png\" alt=\"table icon\" /> " . $title . "</td></tr></thead>\n"
. "<tbody id=\"" . $title . "_" . $id . "\">\n"
. "<tr bgcolor=\"" . $bgcolor2 . "\"><td class=\"thead\">" . $date . " - " . $time . " - by " . $author . "</td></tr>\n"
. "<tr bgcolor=\"" . $bgcolor3 . "\" style=\"color:#FFF\"><td><table width=\"100%\"><tr bgcolor=\"" . $bgcolor4 . "\" style=\"color:#000\"><td>\n"
. "<div id=\"ttext\" style=\"padding-top:5px; padding-bottom:5px; padding-left:5px; padding-right:5px; width:100%\">" . $text . "</div>\n"
. "</td></tr></table><span style=\"color:#CCC\">...[Read More]</span></td></tr>\n";
echo "<tr bgcolor=" . $bgcolor1 . "><td class=\"tfooter\">Hello</td></tr></tbody></table>\n"
. "</div>\n";
};
?>Code: Select all
function delnews(nid, title)
{
if (confirm("Are you sure you want to delete '" + title + "'"))
{
window.location.href = 'index.php?delnews=' + nid;
}
}Code: Select all
Warning: Cannot modify header information - headers already sent by (output started at /home/insight/public_html/includes/header.php:18) in /home/insight/public_html/modules/news/index.php on line 36
Code: Select all
header('Location: ' . $_SERVER['HTTP_REFERER']);So can somebody please tell me what I've done wrong? How do I get it to reload the page once I've clicked the delete button and how do I get it to show all the content on the page? And why isn't it deleting the table from the database?