Jump to content

Deleting a row from the database. *SOLVED*


pocobueno1388

Recommended Posts

Sanfly - Your code won't delete any messages. Here is the code with you stuff added:

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

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>";
}



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]

and here is the debug stuff that was displayed.

[code]We've selected some items to delete

ARRAY: delete

1[/code]
Link to comment
Share on other sites

Aahhhhhh, I see now

You have the same "name" for your checkboxes and the delete submit button

Change this line in the if statement

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

to

[quote]$delMessages = $_POST[color=red]['delMsg'][/color];  // Get the array[/quote]

And change this line in the other part of the code

[quote]echo '<td align="center" bgcolor="788D7A"><input type="checkbox" name="delete[]" value="' .  $row['messageID'] . '"></td>';[/quote]

to

[quote]echo '<td align="center" bgcolor="788D7A"><input type="checkbox" [color=red]name="delMsg[]"[/color] value="' .  $row['messageID'] . '"></td>';[/quote]

Let me know how it goes
Link to comment
Share on other sites

Sanfly- It is almost working now. It will delete as many messages as checked, but it deletes starting from the top. So if there were 3 messages in the inbox, and the user only checked the middle or last one, it would delete the first message.

So now all that needs to be done, is make the script identify which message needs to be deleted. Here is the updated code:

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

if ($_POST['delete']){

$delMessages = $_POST['delMsg'];  // $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'";
         

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="delMsg[]" 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]
Link to comment
Share on other sites

heres my inbox for a pm system! it has a delete system in it!

It deletes multiple messages
EDIT: I deleted alot of the stuff to narrow down code

[code]
$delete = array();

echo "<form method='POST'>";

if (mysql_num_rows($result2) > 0) {
while($row = mysql_fetch_row($result2)){

echo "<b><a href='pm.php?id=$row[0]'>$row[3]</a><input type='checkbox' name='delete[]' value='$row[0]'>
</b><font size='1'><br> From: $row[2]</font><br><br>";

echo "<input type='submit' name='sub' value='Delete'></form>";
if(isset($_POST['sub'])){
$delete=$_POST['delete'];
foreach($delete as $key => $value){
$query = "DELETE FROM Pmess WHERE id='$value' LIMIT 1";
$result = mysql_query($query) or die ("Error in query: $query2. ".mysql_error());
echo "Message(s) Deleted(You may need to refresh page in order to see changes)";
[/code]
Link to comment
Share on other sites

Like I said earlier,

[quote]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?[/quote]

So try going back to my if statement, with the delMsg change added

[code]if ($_POST['delete']){
$delMessages = $_POST['delMsg'];  // Get the array
$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
}
}
[/code]

Better?
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.