PDA

View Full Version : deleting a row


fleep
07-04-06, 05:20 PM
This is supposed to allow members to view and delete their messages stored on a MySQL database.


<?php
require('config.php');

require('functions.php');

if (!$_SESSION[user_name])
{
header('Location:login.html');
exit;
}
?>

<?php
$connection = @mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)or die(mysql_error());


$dsql = "SELECT message, sender, contact, date_sent, mess_id FROM messages WHERE id = '$id' ORDER BY date_sent DESC";
$mesresults = mysql_result(mysql_query("SELECT COUNT(mess_id) as Num FROM messages where id = '$id'"), 0);?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">

<?php

$mets = @mysql_query($dsql, $connection) or die(mysql_error());
while ($dsql = mysql_fetch_object($mets))
{
$message = $dsql -> message;
$sender = $dsql -> sender;
$contact = $dsql -> contact;
$date = $dsql -> date_sent;
$mess_id = $dsql -> mess_id;

echo "<div id=\"mes_borders\">";
echo "<table border=\"0\" width=\"590\" id=\"table5\">";
echo "<tr>";
echo "<td width=\"587\"><img src=\"message_pic.gif\"> from <b>$sender.$mess_id</b> Sent on the $date<p align=\"justify\">$message</p><p><b>email/phone</b> $contact<br/><input name=\"submit\" type=\"submit\" value=\"delete\"><input type=hidden name=\"mess_id\" value=\"$mess_id\" </p></td>";
echo "</table>";
echo "</div>";
echo "<p></p>";


}




if (submit)
{
$sql = "DELETE FROM messages WHERE mess_id ='$mess_id'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
}



?>

The delete message bit isn't working as it should.

Nico
07-04-06, 05:23 PM
Moved to database.

EDIT:

And I guess the error is here


if (submit)


Try this instead

if (isset($_POST['submit']))

Oras
07-04-06, 05:47 PM
It is not working because this code never excuted:

if (submit)
{
$sql = "DELETE FROM messages WHERE mess_id ='$mess_id'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
}

what is submit? you should make it like this:

if ($_POST)
{
$sql = "DELETE FROM messages WHERE mess_id ='$mess_id'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
}

and for nico_swd , its a php issue not db :) you may return it to php section
regards

fleep
07-04-06, 06:14 PM
thanks for your replies but neither suggestions worked. It is still doing exactly what it did before.

Here's what is suppose to happen.
Messages are displayed. Each message has a corresponding delete(submit) button. I click on the button to delete that particular message.

Here's what is actually happening.
I have to click twice on a the delete button before a message is deleted and furthermore when it does delete, it deletes the last message in the list regardless which button I pressed.

Oras
07-04-06, 06:50 PM
aha ... this is impossible in your way :) you are putting more than submit button for the same form!
The right way to do this is by replacing submit by url like this: delete.php?id=$id
and in delete.php page get the id and apply the query
wish this help

fleep
07-04-06, 06:54 PM
Thanks Oras!

That makes sense. I'll give this a try!

fleep
07-04-06, 07:55 PM
Bingo!
thnks again!