Jump to content


Photo

Deleting a row from the database. *SOLVED*


  • Please log in to reply
31 replies to this topic

#21 vinny_bc

vinny_bc
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 July 2006 - 06:00 AM

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.

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


#22 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 27 July 2006 - 06:12 AM

Here is the error I got:

DELETE FROM message WHERE messageID = 'd'


#23 vinny_bc

vinny_bc
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 July 2006 - 06:17 AM

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.



#24 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 27 July 2006 - 06:30 AM

Woah, I got it to delete messages now, it just only deletes one message at a time. Here is the 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;

?>

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

#25 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 July 2006 - 08:28 AM

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

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

copy and paste exactly what show up on the next page
If you're not part of the solution, you're part of the precipitate

#26 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 27 July 2006 - 05:15 PM

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

<?
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;

?>

and here is the debug stuff that was displayed.

We've selected some items to delete

ARRAY: delete

1


#27 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 July 2006 - 07:35 PM

Aahhhhhh, I see now

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

Change this line in the if statement

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


to

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


And change this line in the other part of the code

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


to

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


Let me know how it goes
If you're not part of the solution, you're part of the precipitate

#28 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 28 July 2006 - 02:00 AM

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:

<?
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;

?>


#29 legohead6

legohead6
  • Members
  • PipPipPip
  • Advanced Member
  • 434 posts

Posted 28 July 2006 - 02:06 AM

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

$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)";

"Syntax error" .. WHERE THE **** IS IT?!

#30 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 28 July 2006 - 02:12 AM

Like I said earlier,

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?


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

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

Better?
If you're not part of the solution, you're part of the precipitate

#31 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 28 July 2006 - 02:35 AM

Thanks so much Sanfly! The code now works, and everything is fixed.

Thanks to everyone who took their time to help XD I really appreciate it.

#32 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 28 July 2006 - 03:45 AM

Your welcome :)

Edit your first post and add *SOLVED* to the title
If you're not part of the solution, you're part of the precipitate




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users