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

MySql Delete Comment

Post by JustinMs66 »

i have working code that will display row entries in a database.
what i am now trying to add, is a picture of a trash can, and when you click on it, it will delete that comment!

here is my current code:

Code: Select all

<?php
//connect to database, select table
$db = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

// show entries
$query  = "SELECT csubject, cmessage FROM deltest1";
$result = mysql_query($query) or die('Error, query failed ##1');

// this defines html code i want for the entries. tables... etc.
$htmlsubbefore="(html table code...etc)";
$htmlsubafter="(html table code...etc)";
$htmlbodbefore="(html table code...etc)";

// // // MY DELETE CODE!!!
if(isset($_GET['act']) && $_GET['act'] == 'del'){
 $sql_delete = "DELETE FROM deltest1 WHERE cmessage=$id";
 mysql_query($sql_delete) or die(mysql_error()); 
}
// // // END DELETE CODE

echo "<table>";
while($row = mysql_fetch_row($result))
{
    $Subject    = $row[0];
    $Message = $row[1];

//display the entries
    echo $htmlsubbefore . $row[0] . $htmlsubafter.
         $htmlbodbefore . $row[1] . $htmlbodafter . "</table>";

// NOW here is where i display my trash can image:
echo "<a href='delete3.php?act=del&id=" . $MESSAGE_ID . "' title='Delete'><img src=\"http://image.fpsbanana.com/ico/del.gif\"></a>";
// End trash can image code

}
// more html crap:
   $htmlbodafter="</td>/tr></table>";
?>
now when i use that code, i get this error when i click the trash can:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
please help?
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Re: MySql Delete Comment

Post by hrubos »

I think here and change

Code: Select all

$sql_delete = "DELETE FROM deltest1 WHERE cmessage='$id' "
and I haven't seen where do [s]u[/s] you have $id ???
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Where you define $id???

And use `` for tables and columns:

Code: Select all

DELETE FROM `deltest1` WHERE `cmessage`='$id'
hrubos
Forum Contributor
Posts: 172
Joined: Sat Oct 07, 2006 3:44 pm

Post by hrubos »

ok wrote:Where you define $id???

And use `` for tables and columns:

Code: Select all

DELETE FROM `deltest1` WHERE `cmessage`='$id'
:) hi
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok wrote:1) where you define $id???
2) use `` for tables and columns:
hmmmm very good point. i copied this code from a tutorial and modd'd it for my liking, and i'm not really sure where that is. anyway, how would i get the Message ID!??
2) ok
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

ok wrote:Where you define $id???

And use `` for tables and columns:

Code: Select all

DELETE FROM `deltest1` WHERE `cmessage`='$id'
It looks like hes not verifying the data at all, and has register_globals on.... *cringe* :roll:
Last edited by Zoxive on Mon Dec 04, 2006 3:28 pm, edited 2 times in total.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

ok wrote:1) where you define $id???
hmmmm very good point. i copied this code from a tutorial and modd'd it for my liking, and i'm not really sure where that is. anyway, how would i get the Message ID!??
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

JustinMs66 wrote:
ok wrote:1) where you define $id???
2) use `` for tables and columns:
hmmmm very good point. i copied this code from a tutorial and modd'd it for my liking, and i'm not really sure where that is. anyway, how would i get the Message ID!??
2) ok

Code: Select all

$id = $_GET['id'];
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You need to get it from the user or if you want to delete specific message, you can mention it in the PHP code.

Code: Select all

<?php
$sql_delete = "DELETE FROM `deltest1` WHERE `cmessage`=$_GET['cid']";
mysql_query($sql_delete) or die(mysql_error()); 
?>
P.S
`cmessage` most be UNIQUE, else it can delete more then one record(comment).
I assumed that `cmessage` is the message id.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

Everah wrote:

Code: Select all

$id = $_GET['id'];
ok i added that above the delete code.
i still get this:
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

if(isset($_GET['act']) && $_GET['act'] == 'del' && is_int($_GET['id'])){
 $sql_delete = sprintf("DELETE FROM `deltest1` WHERE `cmessage`='%s'",mysql_real_escape_string($_GET['id']));
 mysql_query($sql_delete) or die(mysql_error());
}
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

Zoxive wrote:

Code: Select all

if(isset($_GET['act']) && $_GET['act'] == 'del' && is_int($_GET['id'])){
 $sql_delete = sprintf("DELETE FROM `deltest1` WHERE `cmessage`='%s'",mysql_real_escape_string($_GET['id']));
 mysql_query($sql_delete) or die(mysql_error());
}
ok i tried that and i get this error:
  • Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/.hobbie/justinms66/csscobalt.com/to_do_list_fpsb/t1/delete5.php on line 115
Last edited by JustinMs66 on Mon Dec 04, 2006 3:38 pm, edited 1 time in total.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Code: Select all

<?php
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);
}
?>
Assuming this file called index.php, you need to call it like that: index.php?id=x (where x is an integer).

Run the above and post the output.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Is your code in delete3.php? You are telling the link to send 'act=del' & 'id=" . $MESSAGE_ID . "' at the page delete3.php. So is all this code on delete 3.php?
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

Everah wrote:Is your code in delete3.php?
omg. i totaly forgot. every time i work on it, i save it as the next number, so i can go back if needed. so it was delete5.php :oops: anyway, i changed it, and now all it does is refresh the page.
ok wrote:php...
when i do that, i get this:
  • No id



also, maybe i wasn't clear.
inside my phptest1 database, i have a "deltest1" table. and inside that, i have "cmessage", "csubject", and "cid". now when i create an entry, i use "cmessage", and "csubject". and i didn't realize til now that cid would be used for the the message id -_-
Post Reply