Jump to content

Archived

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

pocobueno1388

Deleting a row from the database. *SOLVED*

Recommended Posts

I created a script where users can send messages to eachother back and forth, like a PM type thing. I have it so it displays all the messages in their inbox and has a checkbox that if they check it and press delete it is supposed to delete the message. The problem I am having is it won't delete the message when the click delete. Here is the script:

[code]<?
include 'header.php';

if ($_POST['delete']){
mysql_query("DELETE * FROM message WHERE messageID='$delete'");
}



print<<<HERE

<form action="inbox.php?action=delete">
<center><h2>INBOX</h2><p>
<h3><a href="sendmessage.php">Send a message</a></h3>
</center>

<table border=1 bordercolor="black" width="85%" align="center">

<th align="center" bgcolor="49614A">Delete?</th>
<th align="center" bgcolor="49614A">Subject</th>
<th align="center" bgcolor="49614A">From</th>
<th align="center" bgcolor="49614A">Time</th>
<th align="center" bgcolor="49614A">Read?</th>

</tr>
HERE;

$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";
$result1= mysql_query($sql1);
$row1 = mysql_fetch_assoc($result1);

$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE

receiver='$sid'";
$result= mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete"></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID']

. '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' .

$row['senderID'] . '">' .  $row['sender'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';

if ($row['read'] == no){
        echo '<td align="center"><font color="red">' . $row['read'] . '</font></td></tr>';
} else {
echo '<td align="center" bgcolor="788D7A"><font color="C8F6C8">' . $row['read'] .

'</font></td></tr>';

}//end if
}//end while


print<<<HERE

</table>
<p>

<table width="85%" align="center">

<td align="left"><input type="submit" name="delete" value="delete"></td>
</table>
</form>

HERE;

?>[/code]

The delete boxes and codes are all in different spots in the script, so I thought it would be easier if I just posted the entire script. Any help would be appreciated =D Thanks.

Share this post


Link to post
Share on other sites
if ($_POST['delete']){
mysql_query("DELETE * FROM message WHERE messageID='$delete'");
}

to

if ($_POST['delete']){
mysql_query("DELETE * FROM message WHERE messageID='".$_POST['delete']."');
}

Share this post


Link to post
Share on other sites
High Tek - I switched my code to your code and the screen went blank, so I tried this:

[code]if ($_POST['delete']){
  mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");
}[/code]

The screen came back, but it still won't delete the message =(

Share this post


Link to post
Share on other sites
Try adding echo "<form action=\"\" method=POST>";  above while ($row = mysql_fetch_assoc($result)){

Share this post


Link to post
Share on other sites
post the code you got so far and add the database lol..........


That a terrable way to delete can not beleve it lol..............

just add a link were the delete button is of example

You got to make a link but do the link to suit your needs ok.

echo"<a href='delete.php'?playerID=$row[senderID]&cmd=delete'>delete</a>":

delete.php
<?

database connection

if($_GET[cmd]==$delete) {
$query="DELETE * FROM message WHERE playerID=$row[senderID]");

$result_mysql($query);

heder("localtion: back_to_where_ever.php";

}

?>

this is an example only but should work with the correct delete settings and id on the link.

good luck.

my way or the high way lol................

Share this post


Link to post
Share on other sites
Code:
[code]<?
include 'header.php';

if ($_POST['delete']){
  mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");
}



print<<<HERE

<form action="inbox.php?action=delete" method="post">
<center><h2>INBOX</h2><p>
<h3><a href="sendmessage.php">Send a message</a></h3>
</center>

<table border=1 bordercolor="black" width="85%" align="center">

<th align="center" bgcolor="49614A">Delete?</th>
<th align="center" bgcolor="49614A">Subject</th>
<th align="center" bgcolor="49614A">From</th>
<th align="center" bgcolor="49614A">Time</th>
<th align="center" bgcolor="49614A">Read?</th>

</tr>
HERE;

$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";
$result1= mysql_query($sql1);
$row1 = mysql_fetch_assoc($result1);

$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";
$result= mysql_query($sql);

while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete"></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' .  $row['sender'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';

if ($row['read'] == no){
        echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] . '</font></td></tr>';
} else {
echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] . '</font></td></tr>';

}//end if
}//end while


print<<<HERE

</table>
<p>

<table width="85%" align="center">

<td align="left"><input type="submit" name="delete" value="delete"></td>
</table>
</form>

HERE;

?>[/code]

I don't think there is a need for the database stuff...there should be enough information there.

Share this post


Link to post
Share on other sites
Your checkbox should be of the form:

<input type="checkbox" name="delete[]" value="$messageID">

Then $_POST['delete'] will be an array of checked $messageIDs.
Note: if the user didn't check anything, then calling isset($_POST['delete']) will return FALSE.

Share this post


Link to post
Share on other sites
Vinny_bc - That didn't work either =/

Here is the updated code:

[code]<?
include 'header.php';

if ($_POST['delete']){
  mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");
}



print<<<HERE

<form action="inbox.php?action=delete" method="post">
<center><h2>INBOX</h2><p>
<h3><a href="sendmessage.php">Send a message</a></h3>
</center>

<table border=1 bordercolor="black" width="85%" align="center">

<th align="center" bgcolor="49614A">Delete?</th>
<th align="center" bgcolor="49614A">Subject</th>
<th align="center" bgcolor="49614A">From</th>
<th align="center" bgcolor="49614A">Time</th>
<th align="center" bgcolor="49614A">Read?</th>

</tr>
HERE;

$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";
$result1= mysql_query($sql1);
$row1 = mysql_fetch_assoc($result1);

$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE

receiver='$sid'";
$result= mysql_query($sql);

while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value='.

$row['messageID'] .'></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID']

. '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' .

$row['senderID'] . '">' .  $row['sender'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';

if ($row['read'] == no){
        echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] .

'</font></td></tr>';
} else {
echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] .

'</font></td></tr>';

}//end if
}//end while


print<<<HERE

</table>
<p>

<table width="85%" align="center">

<td align="left"><input type="submit" name="delete" value="delete"></td>
</table>
</form>

HERE;

?>[/code]

Share this post


Link to post
Share on other sites
You will need to change your SQL code from:

if ($_POST['delete']){
  mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");
}

To:

if(isset($_POST['delete'])){
  foreach($_POST['delete'] as $i => $message_id){
    mysql_query("DELETE * FROM message WHERE messageID='$message_id'");
  }
}

Remember, $_POST['delete'] is now an array and not a scalar value.

If this doesn't work either, can you please post the HTML that is output by your script - there may still be issues with how your form is being output as HTML.

Cheers

Share this post


Link to post
Share on other sites
correct

i want to see your delete query

the where cluase might and think is wrong ok.

you can make it a lot simple then above use a session example

<? session_start();

$id=$_SESSION['id']=$row['message_id'];

then in the delete where clause use this ok

if(isset($_POST['delete'])){

    mysql_query("DELETE * FROM message WHERE messageID='$id'");
  }
}



Share this post


Link to post
Share on other sites
vinney_bc - That didn't work either =/

redarrow - I don't think I want to do the session idea. It is to confusing, and I already have sessions set up in the header file.

New code:

[code]<?
include 'header.php';

if(isset($_POST['delete'])){
  foreach($_POST['delete'] as $i => $message_id){
    mysql_query("DELETE * FROM message WHERE messageID='$row[messageID]'");
  }
}



print<<<HERE

<form action="inbox.php?action=delete" method="post">
<center><h2>INBOX</h2><p>
<h3><a href="sendmessage.php">Send a message</a></h3>
</center>

<table border=1 bordercolor="black" width="85%" align="center">

<th align="center" bgcolor="49614A">Delete?</th>
<th align="center" bgcolor="49614A">Subject</th>
<th align="center" bgcolor="49614A">From</th>
<th align="center" bgcolor="49614A">Time</th>
<th align="center" bgcolor="49614A">Read?</th>

</tr>
HERE;

$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";
$result1= mysql_query($sql1);
$row1 = mysql_fetch_assoc($result1);

$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE

receiver='$sid'";
$result= mysql_query($sql);

while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value='.

$row['messageID'] .'></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID']

. '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' .

$row['senderID'] . '">' .  $row['sender'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';

if ($row['read'] == no){
        echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] .

'</font></td></tr>';
} else {
echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] .

'</font></td></tr>';

}//end if
}//end while


print<<<HERE

</table>
<p>

<table width="85%" align="center">

<td align="left"><input type="submit" name="delete" value="delete"></td>
</table>
</form>

HERE;

?>[/code]

What do you guys mean by my 'delete query'? Is it now in the script?

Share this post


Link to post
Share on other sites
You didn't follow my code exactly :)

You had:

[code]if(isset($_POST['delete'])){
  foreach($_POST['delete'] as $i => $message_id){
    mysql_query("DELETE * FROM message WHERE messageID='[color=red][b]$row[messageID][/b][/color]'");
  }
}[/code]

Whereas I had:

[code]if(isset($_POST['delete'])){
  foreach($_POST['delete'] as $i => $message_id){
    mysql_query("DELETE * FROM message WHERE messageID='[b]$message_id[/b]'");
  }
}[/code]

Share this post


Link to post
Share on other sites
can you post your code as it stands at the moment.
And can you also give us a sample of your DB.

Share this post


Link to post
Share on other sites
My code is where exactly how it is when I first posted. There were so many ideas flying around, and I couldn't get any to work, so I guess we will have to start fresh again. So here is the code again:

[code]<?
include 'header.php';

if ($_POST['delete']){
mysql_query("DELETE * FROM message WHERE messageID='$delete'");
}



print<<<HERE

<form action="inbox.php?action=delete">
<center><h2>INBOX</h2><p>
<h3><a href="sendmessage.php">Send a message</a></h3>
</center>

<table border=1 bordercolor="black" width="85%" align="center">

<th align="center" bgcolor="49614A">Delete?</th>
<th align="center" bgcolor="49614A">Subject</th>
<th align="center" bgcolor="49614A">From</th>
<th align="center" bgcolor="49614A">Time</th>
<th align="center" bgcolor="49614A">Read?</th>

</tr>
HERE;

$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";
$result1= mysql_query($sql1);
$row1 = mysql_fetch_assoc($result1);

$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE

receiver='$sid'";
$result= mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete"></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID']

. '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' .

$row['senderID'] . '">' .  $row['sender'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';

if ($row['read'] == no){
        echo '<td align="center"><font color="red">' . $row['read'] . '</font></td></tr>';
} else {
echo '<td align="center" bgcolor="788D7A"><font color="C8F6C8">' . $row['read'] .

'</font></td></tr>';

}//end if
}//end while


print<<<HERE

</table>
<p>

<table width="85%" align="center">

<td align="left"><input type="submit" name="delete" value="delete"></td>
</table>
</form>

HERE;

?>[/code]

Here is the database information:

Table name - message

Fields:
messageID
sender
receiver
senderID
subject
body
time
read

Share this post


Link to post
Share on other sites
Okay, I see two things wrong here.  Of course I could be completely wrong on either of these, but anyway....


1.  In the delete query

[code]mysql_query("DELETE * FROM message WHERE messageID='$delete'");[/code]

you dont need the *, infact if i remember correctly, it prevents the delete query from working. 

2.  In the checkbox to delete, I dont think your passing the value for the messageID

[code]<input type="checkbox" name="delete">[/code]

I dont use checkboxes that often, I may be wrong here, but try adding the messageID as the value

[code]<input type="checkbox" name="delete" value="' . $row['messageID'] . '">[/code]

Also, Im assuming this is generating a list of messages, and you can select more than one to delete?  If this is the case, you will need to change the delete checkbox to this:

[code]<input type="checkbox" name="delete[]" value="' . $row['messageID'] . '">[/code]

([] after the name, puts the values into an array)

and then when you submit the form, you need to extract the values as an array, and use either a for or foreach loop (I always have trouble with foreach, and forget when it can and cant be used so it might be better to use count() to find out how many items in the array, then use a for loop) to delete each one

ROUGH EXAMPLE, WILL NEED REFINING, IS ONLY HERE TO GIVE YOU AN IDEA:

[code]if ($_POST['delete']){
$delMessages = $_POST['delete'];  // $delMessages or maybe $delMessages[] ?????
$num = count($delMessages);
for($i = 0; $i < $num; $i++){
$messageID = $delMessages[$i];  // not so sure on this....
mysql_query("DELETE FROM message WHERE messageID = '$messageID'");
}
}[/code]

Share this post


Link to post
Share on other sites
Sanfly - I put in everything you had put, and I couldn't get it to work. Here is the code with all your stuff added:

[code]<?
include 'header.php';

if ($_POST['delete']){
$delMessages = $_POST['delete'];  // $delMessages or maybe $delMessages[] ?????
$num = count($delMessages);
for($i = 0; $i < $num; $i++){
$messageID = $delMessages[$i];  // not so sure on this....
mysql_query("DELETE FROM message WHERE messageID = '$messageID'");
}
}



print<<<HERE

<form action="inbox.php?action=delete" method="post">
<center><h2>INBOX</h2><p>
<h3><a href="sendmessage.php">Send a message</a></h3>
</center>

<table border=1 bordercolor="black" width="85%" align="center">

<th align="center" bgcolor="49614A">Delete?</th>
<th align="center" bgcolor="49614A">Subject</th>
<th align="center" bgcolor="49614A">From</th>
<th align="center" bgcolor="49614A">Time</th>
<th align="center" bgcolor="49614A">Read?</th>

</tr>
HERE;

$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";
$result1= mysql_query($sql1);
$row1 = mysql_fetch_assoc($result1);

$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE

receiver='$sid'";
$result= mysql_query($sql);

while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value="' .

$row['messageID'] . '"></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID']

. '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' .

$row['senderID'] . '">' .  $row['sender'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';

if ($row['read'] == no){
        echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] .

'</font></td></tr>';
} else {
echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] .

'</font></td></tr>';

}//end if
}//end while


print<<<HERE

</table>
<p>

<table width="85%" align="center">

<td align="left"><input type="submit" name="delete" value="delete"></td>
</table>
</form>

HERE;

?>[/code]

Here is the original code again, if anyone else would like to give it a shot:

[code]<?
include 'header.php';

if ($_POST['delete']){
  mysql_query("DELETE * FROM message WHERE messageID='$_POST[delete]'");
}



print<<<HERE

<form action="inbox.php?action=delete" method="post">
<center><h2>INBOX</h2><p>
<h3><a href="sendmessage.php">Send a message</a></h3>
</center>

<table border=1 bordercolor="black" width="85%" align="center">

<th align="center" bgcolor="49614A">Delete?</th>
<th align="center" bgcolor="49614A">Subject</th>
<th align="center" bgcolor="49614A">From</th>
<th align="center" bgcolor="49614A">Time</th>
<th align="center" bgcolor="49614A">Read?</th>

</tr>
HERE;

$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";
$result1= mysql_query($sql1);
$row1 = mysql_fetch_assoc($result1);

$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE receiver='$sid'";
$result= mysql_query($sql);

while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete"></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID'] . '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' . $row['senderID'] . '">' .  $row['sender'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';

if ($row['read'] == no){
        echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] . '</font></td></tr>';
} else {
echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] . '</font></td></tr>';

}//end if
}//end while


print<<<HERE

</table>
<p>

<table width="85%" align="center">

<td align="left"><input type="submit" name="delete" value="delete"></td>
</table>
</form>

HERE;

?>[/code]

Share this post


Link to post
Share on other sites
I think u'll need to provide us with some debug messages so that we can figure out what's going on

Try modifying the code to this so that it's outputting some debug statements.  Then copy and paste the statements into a reply so that we can have a look.

[code=php:0]
if ($_POST['delete']){
$delMessages = $_POST['delete'];  // $delMessages or maybe $delMessages[] ?????
$num = count($delMessages);
for($i = 0; $i < $num; $i++){
$messageID = $delMessages[$i];  // not so sure on this....
                $sql = "DELETE FROM message WHERE messageID = '$messageID'";
                echo $sql.'<BR><BR>';
mysql_query($sql);
}
}
[/code]

Share this post


Link to post
Share on other sites
is messageID meant to be a letter or a number?

If it's meant to be a number, then the problem is you are writing the wrong row value as the value of your checkbox.

If it's meant to be a letter, than can you provide us with some sample data from your table.

thanks.

Share this post


Link to post
Share on other sites
Woah, I got it to delete messages now, it just only deletes one message at a time. Here is the code:

[code]<?
include 'header.php';

if ($_POST['delete']){



$delMessages = $_POST['delete'];  // $delMessages or maybe $delMessages[] ?????



$num = count($delMessages);



for($i = 0; $i < $num; $i++){


$sql6 = "SELECT `messageID` FROM message WHERE receiver='$sid'";
$result6= mysql_query($sql6);
$row6 = mysql_fetch_assoc($result6);


$messageID = $row6['messageID'];  // not so sure on this....
                $sql = "DELETE FROM message WHERE messageID = '$messageID'";
                echo $sql.'<BR><BR>';





mysql_query($sql);



}
}



print<<<HERE

<form action="inbox.php?action=delete" method="post">
<center><h2>INBOX</h2><p>
<h3><a href="sendmessage.php">Send a message</a></h3>
</center>

<table border=1 bordercolor="black" width="85%" align="center">

<th align="center" bgcolor="49614A">Delete?</th>
<th align="center" bgcolor="49614A">Subject</th>
<th align="center" bgcolor="49614A">From</th>
<th align="center" bgcolor="49614A">Time</th>
<th align="center" bgcolor="49614A">Read?</th>

</tr>
HERE;

$sql1 = "SELECT `playerID` FROM players WHERE playerID='$row[senderID]'";
$result1= mysql_query($sql1);
$row1 = mysql_fetch_assoc($result1);

$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read`, `senderID` FROM message WHERE

receiver='$sid'";
$result= mysql_query($sql);

while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value="' .

$row['messageID'] . '"></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="sendmessage.php?id=' . $row['messageID']

. '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A"><a href="viewprofile.php?profileid=' .

$row['senderID'] . '">' .  $row['sender'] . '</a></td>';
        echo '<td align="center" bgcolor="788D7A">' . $row['time'] . '</td>';

if ($row['read'] == no){
        echo '<td align="center" bgcolor="788D7A"><font color="5D0B0B">' . $row['read'] .

'</font></td></tr>';
} else {
echo '<td align="center" bgcolor="788D7A"><font color="1D221D">' . $row['read'] .

'</font></td></tr>';

}//end if
}//end while


print<<<HERE

</table>
<p>

<table width="85%" align="center">

<td align="left"><input type="submit" name="delete" value="delete"></td>
</table>
</form>

HERE;

?>
[/code]

Any ideas on how to get it to delete every message that is checkmarked?

Share this post


Link to post
Share on other sites
That code you wrote is not even taking the checked items into account, its probably just deleting the first message whether selected or not, right?

Try replacing that if statement with this

[code]if ($_POST['delete']){
echo "We've selected some items to delete<br><br>";  // Debug code, can be removed later

$delMessages = $_POST['delete'];  // Get the array

echo "ARRAY: "; print_r($delMessages); echo "<br><br>";  // dislaying the array for us, can be removed later

$num = count($delMessages); // Count how many items in the array

for($i = 0; $i < $num; $i++){ // beginning of the for loop to run through all the array items

$messageID = $delMessages[$i];  // Sets the variable $messageID for the selected message

$r = mysql_query("DELETE FROM message WHERE messageID = '$messageID'") or die(mysql_error());  // you should always include a die statement when trying to debug mysql

echo "$r<br><br>";  // display the query so we can see if there is anything going wrong, can be removed later
}
}
else{
echo "Nothing Selected for deletion<br><br>";
}[/code]

copy and paste exactly what show up on the next page

Share this post


Link to post
Share on other sites

×

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.