Jump to content

DELETE FROM syntax problem


randal_138

Recommended Posts

My delete button will not work for some reason.  I have created a loop, ordered my database entries with $i and have gotten closer, but not 100% of the way to making it work.  I am trying to delete rows from my database individually.  Not sure where I am going wrong with my syntax, but something is not working.  The entries are not deleting nor am I receiving any errors.  I am stumped.  Here is the code.  I shortened it for readability.

 


<?php
// database connection information
include('connection.php');

$query = "SELECT * FROM FORUM_MESSAGE ORDER BY MSG_DATE DESC";
$result = mysql_query($query);
$num = mysql_num_rows($result);

$i = 0;
while ($i < $num)
{
$subject = mysql_result ($result, $i, "SUBJECT");
$message_text = mysql_result ($result, $i, "MSG_TEXT");	

echo '<form method="POST" name="subject">';
echo '<input type="hidden" name="update" value="<?php echo $subject; ?>" />';
echo '<input type="submit" name="update" value="Update" />';
echo '         ';
echo '<input type="hidden" name="delete" value="' . $subject . '" />';
echo '<input type="submit" value="Delete" />';
echo '</form>';

$i++;
}

if (isset($_POST['delete']))
{
include ('connection.php');
mysql_query("DELETE FROM FORUM_MESSAGE WHERE SUBJECT = " . $subject);
}

?>

</body>
</html>

 

Thanks again!

 

Ryan

Link to comment
Share on other sites

I modified it so that it would be easier to read with only what I need to submit.  I did try this:

 


<?php
include('connection.php');

$query = "SELECT * FROM FORUM_MESSAGE ORDER BY MSG_DATE DESC";
$result = mysql_query($query);
$num = mysql_num_rows($result);

mysql_close();

$i = 0;
while ($i < $num)
{
$subject = mysql_result ($result, $i, "SUBJECT");
$message_text = mysql_result ($result, $i, "MSG_TEXT");

echo '<div style="width: 400px;padding:20px;">';
echo '<table border=0 width="400px">';
echo '<tr>';
echo '<td style="vertical-align:top;width:auto;">';
echo '</td>';
echo '<td style="vertical-align:top;width:320px;text-align:center;">';
echo '<form method="POST" name="subject">';
echo '<input type="hidden" name="update" value="' . $subject . '" />';
echo '<input type="submit" name="update" value="Update" />';
echo '         ';
echo '<input type="hidden" name="delete" value="' . $subject . '" />';
echo '<input type="submit" value="Delete" />';
echo '</form>';
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br />';
echo '<hr />';
echo '</div>';

$i++;
}

if (isset($_POST['delete']))
{
include ('connection.php');
mysql_query("DELETE FROM FORUM_MESSAGE WHERE SUBJECT = " . $subject);
}

?>

 

That is what I am working with right now.  I don't think the $subject and the actual button are connecting, but I can't point what exactly is wrong.

 

Ryan

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.