MySql Delete Comment
Moderator: General Moderators
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
ok well it dosn't work for me, so here is the EXACT code i'm using (to test it)ok wrote:I tested the code that I gave you and it works. So, try again and check that you pass 'id' to the PHP file (?id=1).
Code: Select all
<?php
$dbhost = 'my host';
$dbuser = 'my user';
$dbpass = 'my pass';
$dbname = 'phptest1';
mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Cannot select DB');
if(isset($_GET['id']) and $_GET['id'] != null and is_numeric($_GET['id']))
{
$id = $_GET['id'];
}
else
{
die('No id');
}
$sql_delete = "DELETE FROM `deltest1` WHERE `cmessage`=$id";
$result = mysql_query($sql_delete);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
?>anyway, with that code, i get this:
No id
Last edited by JustinMs66 on Mon Dec 04, 2006 4:16 pm, edited 1 time in total.
Are you putting ?id=104 at the end?
example.. http://www.website.com/delete.php?id=104
104 being the id that will get deleted.
example.. http://www.website.com/delete.php?id=104
104 being the id that will get deleted.
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
yes... here, here is the exact code:
Code: Select all
<?php
$dbhost =
$dbuser =
$dbpass =
$dbname =
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = "SELECT `csubject`, `cmessage` FROM `deltest1`";
$result = mysql_query($query) or die('Error, query failed ##1');
$htmlsubbefore="<div class=\"ts hazardrow\"><div class=\"coltitle_dark tlarge\">";
$htmlsubafter="</div><div style=\"clear:both;padding:0px\"></div></div>";
$htmlbodbefore="<table width=\"100%\" cellpadding=\"1\" cellspacing=\"1\"><tr class=\"hazardrow\"><td width=\"50%\" colspan=\"1\">";
// ID check:
if(isset($_GET['id']) and $_GET['id'] != null and is_numeric($_GET['id']))
{
$id = $_GET['id'];
}
else
{
//die('No id');
}
//end ID check
//deltion check
if(isset($_GET['act']) && $_GET['act'] == 'del'){
$sql_delete = "DELETE FROM `deltest1` WHERE `cmessage`=$id";
mysql_query($sql_delete) or die(mysql_error()); // gives errors
}
// END DELETE CODE
echo "<table>";
while($row = mysql_fetch_row($result))
{
$Subject = $row[0];
$Message = $row[1];
ECHO $row[2];
ECHO $row[3];
echo $htmlsubbefore . $row[0] . $htmlsubafter.
$htmlbodbefore . $row[1] . $htmlbodafter . "</table>";
echo "<a href='delete7.php?act=del&id=" . $id . "' title='Delete'><img src=\"http://image.fpsbanana.com/ico/del.gif\"></a>";
// $MESSAGE_ID $_GET['id'] $id
}
$htmlbodafter="</td>/tr></table>";
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Now I understand. It goes like that:
Code: Select all
//...
$query = "SELECT `cid`, `csubject`, `cmessage` FROM `deltest1`";
$result = mysql_query($query) or die('Error, query failed ##1');
//...
if(isset($_GET['id']) and $_GET['id'] != null and is_numeric($_GET['id']))
{
$id = $_GET['id'];
}
else
{
die('No id');
}
//end ID check
//deltion check
if(isset($_GET['act']) && $_GET['act'] == 'del'){
$sql_delete = "DELETE FROM `deltest1` WHERE `cmessage`=$id";
mysql_query($sql_delete) or die(mysql_error()); // gives errors
}
// END DELETE CODE
echo "<table>";
while($row = mysql_fetch_assoc($result))
{
$Subject = $row["csubject"];
$Message = $row["cmessage"];
//============================
//***********************************
$id = $row["cid"];
//***********************************
//============================
ECHO $row[2];
ECHO $row[3];
echo $htmlsubbefore . $row[0] . $htmlsubafter.
$htmlbodbefore . $row[1] . $htmlbodafter . "</table>";
echo "<a href='delete7.php?act=del&id=" . $id . "' title='Delete'><img src=\"http://image.fpsbanana.com/ico/del.gif\"></a>";
// $MESSAGE_ID $_GET['id'] $id
}
//...- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You might need to speak the logic out loud to yourself so you can understand what you want to take place. You are pulling information from a database table (one piece of which is a record ID) which you are using to populate a list which is used to push a value to a page that receives the data processes a query based on it. Does that make sense to you? If so, code that process I just described.
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
ok first of all, i want to thank ALL of you for your help
i finally got almost everything to work, although i had to tweak it a bit.
here is the code i used:
now their is just 1 more thing. after you click the image, it does delete it, but unless you refresh the page, it still shows the entry. is their a way so that once its deleted, it somehow refreshes but not refreshes? lol i mean like without refreshing the whole page?
i finally got almost everything to work, although i had to tweak it a bit.
here is the code i used:
Code: Select all
<?php
include 'config.php';
mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Cannot select DB');
$query = "SELECT `cid`, `csubject`, `cmessage` FROM `deltest1`";
$result = mysql_query($query) or die('Error, query failed ##1');
if(isset($_GET['id']) and $_GET['id'] != null and is_numeric($_GET['id']))
{
$id = $_GET['id'];
}
else
{
}
if(isset($_GET['act']) && $_GET['act'] == 'del'){
$sql_delete = "DELETE FROM `deltest1` WHERE `cid`=$id";
mysql_query($sql_delete) or die(mysql_error()); // gives errors
}
echo "<table>";
while($row = mysql_fetch_assoc($result))
{
$Subject = $row["csubject"];
$Message = $row["cmessage"];
$id = $row["cid"];
echo $row["csubject"] .
$row["cmessage"];
echo "<a href='delete8.php?act=del&id=" . $id . "' title='Delete'><img src=\"http://image.fpsbanana.com/ico/del.gif\"></a>";
}
?>You mean AJAX: http://developer.mozilla.org/en/docs/AJ ... ng_Started
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
See viewtopic.php?p=335639#335639 for more AJAX links.
- JustinMs66
- Forum Contributor
- Posts: 127
- Joined: Sun Sep 03, 2006 4:18 pm
ok thanks i'l try to do it that way.
but if i cant, whats the PHP code to "Refresh" the page, or redirect?
i assume i would put it here:
but if i cant, whats the PHP code to "Refresh" the page, or redirect?
i assume i would put it here:
Code: Select all
if(isset($_GET['act']) && $_GET['act'] == 'del'){
$sql_delete = "DELETE FROM `deltest1` WHERE `cid`=$id";
mysql_query($sql_delete) or die(mysql_error()); // gives errors
--- REDIRECT CODE HERE ---
}You have to make sure that you don't print nothing before the redirect.
P.S
If you don't have control on the output to the browser before the redirect header, you can put ob_start() in the start of the PHP code and before you redirecr add ob_clean_end().
Code: Select all
if(isset($_GET['act']) && $_GET['act'] == 'del'){
$sql_delete = "DELETE FROM `deltest1` WHERE `cid`=$id";
mysql_query($sql_delete) or die(mysql_error()); // gives errors
header("Location: next.php");
}If you don't have control on the output to the browser before the redirect header, you can put ob_start() in the start of the PHP code and before you redirecr add ob_clean_end().