this code right here :
[syntax=php]if(isset($_POST['postid'])){
$deletepost = "DELETE FROM chat WHERE id='".$postid."' ";
mysql_query($deletepost);
}
?>[/syntax]
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.
[syntax=php]<?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);
}
?>[/syntax]
Wasn't too sure if this was a PHP or SQL related question, so Jacek can feel free to move this if needed / wanted .
MYSQL deletes wrong entries.
Re: MYSQL deletes wrong entries.
jacek wrote:Where do you define $postid ?
line 13
[syntax=php]while($row = mysql_fetch_assoc($queryget))
{
$postid = $row['id'];[/syntax]
Re: MYSQL deletes wrong entries.
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.
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!
Total brainfart!
Edit: It's working like a charm now, thank you