Jump to content

MySQL DELETE


Far Cry

Recommended Posts

Hi there, I currently have this code that is supposed to delete a message according to their id. The ID is sent through a hidden input field, but it's still somehow not parsing correctly. Here it is....

$query = mysql_query("SELECT * FROM messages WHERE to_user='$username' ORDER BY message_id DESC") or trigger_error('Error: '.mysql_error()); 
        $numrows = mysql_num_rows($query);
                                if ($numrows > 0){
                                        
                                        while ($row = mysql_fetch_assoc($query)){
                                                $id = $row['message_id'];
                                                $from = $row['from_user'];
                                                $to = $row['to_user'];
                                                $title = $row['message_title'];
                                                $content = nl2br($row['message_contents']);
                                                $date = $row['date'];
											$read = $row['message_read'];

											echo"
        <center>
<table border='0' width='100%' style='text-align:center;'>
        <tr>";
	if($read == 0){
        echo"<td width='25%'><font color='white'><a href='inbox.php?action=view&mid=$id'><b>$title</b></a><b><i>NEW!</i></b></font></td>";
	}
	  else{
		  echo"<td width='25%'><font color='black'><a href='inbox.php?action=view&mid=$id'>$title</a></font></td>";
	}
        echo"<td width='25%'><font color='black'>$from</font></td>
	<td width='25%'><font color='black'>$date</font></td>
	<td width='25%'><font color='black'>
	<form action='inbox.php' method='post'>
	<input type='submit' name='delete' value='Delete' class='button'>
	<input type='hidden' name='id' value='$id'>
	</form></td>
        </tr>
        </table></center>
        <br>"; 
	if(isset($_POST['delete'])){
		$message_id = $_POST['id'];
		mysql_query("DELETE * FROM messages WHERE message_id='$message_id'");
		echo"<script type='text/javascript'>
		alert('Message deleted!')
		</script>";
	      }
                }
           }

Link to comment
https://forums.phpfreaks.com/topic/225967-mysql-delete/
Share on other sites

The following is the DELETE query syntax definition, with the most commonly use parts in red -

DELETE [LOW_PRIORITY] [QUICK] [iGNORE] FROM tbl_name

    [WHERE where_condition]

    [ORDER BY ...]

    [LIMIT row_count]

 

You need to use some error checking and error reporting logic on ALL the queries you execute in your code. You would have likely gotten an sql syntax error at the * character in your query. A DELETE query does not use a * in it.

Link to comment
https://forums.phpfreaks.com/topic/225967-mysql-delete/#findComment-1166582
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.