MySql Delete Comment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

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).
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

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).
ok well it dosn't work for me, so here is the EXACT code i'm using (to test it)

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);
}
?>
and i mean i''m not exactly sure where ID is getting ID from. like where is it getting the message ID!?

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.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

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.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

well i cant put id=4 because it needs to be "id=$id" because it needs, in the loop, to store the ID of each message or something.

you know?

and currently, all it does is refresh the page when you click the image
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You have it online?
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

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>";
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

A little PHP 101. Values passed by forms are available in the $_POST array. Values passed by Query String (URL) are available in the $_GET array.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

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
} 
//...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

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:

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>";
}
?>
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?
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok wrote:You mean AJAX
oh it would have to be done with AJAX? damn. i am trying to avoid learning ajax lol. a little hard.
if their a tutorial specific to comment updating/refreshing you could link me to?
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

See viewtopic.php?p=335639#335639 for more AJAX links.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

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:

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 ---
}
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You have to make sure that you don't print nothing before the redirect.

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");
}
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().
Post Reply