Page 1 of 1

MYSQL deletes wrong entries.

Posted: Sat May 21, 2011 9:29 pm
by Temor
this code right here :
if(isset($_POST['postid'])){
$deletepost = "DELETE FROM chat WHERE id='".$postid."' ";
mysql_query($deletepost);
}
?>
is supposed to delete the post with the corresponding post id. But instead, it deletes the oldest entry in the database.
Here is the full code for the page if it could help.
<?php
include('includes/connect.php');




$queryget = mysql_query("SELECT * FROM chat ORDER BY id DESC");



while($row = mysql_fetch_assoc($queryget))
{
$postid = $row['id'];
$name = $row['name'];
$message = $row['message'];
$time = $row['time'];

echo"
<div id='name'>
		<b>$name</b> Skrev:</div> 
<div id='message'>
		".strip_tags($message).$postid." <br /></div>
<div id='time'>
		<b> Klockan:  $time</b>
";
if (is_admin(mysql_real_escape_string($_SESSION['username'])) == true){
		echo "
		<form action='index.php?page=chat' method='POST'>
		<input type='hidden' value='".$postid."' name='postid' />
		<input type='submit' value='delete' />
		</form>
		"; }
echo"
</div><hr>
";
		}

if(isset($_POST['postid'])){
$deletepost = "DELETE FROM chat WHERE id='".$postid."' ";
mysql_query($deletepost);
}
?>
Wasn't too sure if this was a PHP or SQL related question, so Jacek can feel free to move this if needed / wanted :).

Re: MYSQL deletes wrong entries.

Posted: Sat May 21, 2011 9:33 pm
by jacek
Where do you define $postid ?

Re: MYSQL deletes wrong entries.

Posted: Sat May 21, 2011 10:13 pm
by Temor
jacek wrote:Where do you define $postid ?
line 13
while($row = mysql_fetch_assoc($queryget))
{
$postid = $row['id'];

Re: MYSQL deletes wrong entries.

Posted: Sat May 21, 2011 10:42 pm
by jacek
Well. After the while loop this variable will hav the value of the last row processed. Did you mean to use the value from $_POST ?

Re: MYSQL deletes wrong entries.

Posted: Sat May 21, 2011 11:00 pm
by Temor
jacek wrote:Well. After the while loop this variable will hav the value of the last row processed. Did you mean to use the value from $_POST ?
Yeah, of course, my bad! :P

Total brainfart!

Edit: It's working like a charm now, thank you :)

Re: MYSQL deletes wrong entries.

Posted: Sat May 21, 2011 11:08 pm
by jacek
Good good :)