mike12255 Posted January 23, 2009 Share Posted January 23, 2009 I have the following code: $insertnew="INSERT INTO newitems (title,author,date,url) VALUES ('$subject','$name','$thedate','$url[0]')"; mysql_query($insertnew) or die ("Could insert the new data!"); $amount= mysql_query("SELECT * FROM newitems"); $num_rows = mysql_num_rows($amount); if ($num_rows > 4){ for ($counter=10; $num_rows > 4; $counter++){ $getdate = "SELECT MAX(date) FROM `newitems`" ; $dateresult = mysql_query($getdate) or die (mysql_error()); $delete = mysql_fetch_row($dateresult); $todelete = "DELETE FROM newitems WHERE date = $delete"; mysql_query($todelete); } } anyway i cant get it to delete i know its passing for if statment because i echo'd my $num_rows and it was greater then four. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/ Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 $todelete = "DELETE FROM newitems WHERE date = '{$delete['date']}'"; Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744606 Share on other sites More sharing options...
mike12255 Posted January 23, 2009 Author Share Posted January 23, 2009 It didnt work :S Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744607 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 sorry, missed the line before change; $delete = mysql_fetch_row($dateresult); $todelete = "DELETE FROM newitems WHERE date = $delete"; to $delete = mysql_fetch_assoc($dateresult); $todelete = "DELETE FROM newitems WHERE date = {$delete['date']}"; Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744613 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 And the reason why is because $delete = mysql_fetch_assoc($dateresult); returns an array ($delete) with the results. $delete['date'] is encapsulated with { } so that it allows single quotes inside single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744618 Share on other sites More sharing options...
mike12255 Posted January 23, 2009 Author Share Posted January 23, 2009 @ gevans Dont say sorry mate, your helping me. P.S it didnt work @Maq thanks for the explination mate Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744622 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 You need single quotes around the date comparator. That's the whole point of the curly brackets... $todelete = "DELETE FROM newitems WHERE date = '{$delete['date']}'"; Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744623 Share on other sites More sharing options...
mike12255 Posted January 23, 2009 Author Share Posted January 23, 2009 yeah i added it in, already and it still dosnt work: if ($num_rows > 4){ for ($counter=10; $num_rows > 4; $counter++){ $getdate = "SELECT MAX(date) FROM `newitems`" ; $dateresult = mysql_query($getdate) or die (mysql_error()); $delete = mysql_fetch_assoc($dateresult); $todelete = "DELETE FROM newitems WHERE date = {$delete['date']}"; Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744629 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 Ok the sql looks fine. Echo the query, try it in phpmyadmin. try this: $todelete = "DELETE FROM `newitems` WHERE `date` = '".$delete['date']."'"; Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744633 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 <?php if ($num_rows > 4){ for ($counter=10; $num_rows > 4; $counter++){ $getdate = "SELECT MAX(date) AS date FROM `newitems`"; $dateresult = mysql_query($getdate) or die (mysql_error()); $delete = mysql_fetch_assoc($dateresult); $todelete = "DELETE FROM newitems WHERE date = '{$delete['date']}'"; Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744634 Share on other sites More sharing options...
mike12255 Posted January 23, 2009 Author Share Posted January 23, 2009 This kind of worked: if ($num_rows > 4){ for ($counter=10; $num_rows > 4; $counter++){ $getdate = "SELECT MAX(date) AS date FROM `newitems`"; $dateresult = mysql_query($getdate) or die (mysql_error()); $delete = mysql_fetch_assoc($dateresult); $todelete = "DELETE FROM newitems WHERE date = '{$delete['date']}'"; mysql_query($todelete); but it deleted everything instead of leaving me with the four newest Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744644 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 it deleted every row set? Try the query in phpmyadmin, echo the query,. you need to debug a little yourself or you may be on here a long time . Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744652 Share on other sites More sharing options...
mike12255 Posted January 23, 2009 Author Share Posted January 23, 2009 yeah i know i saw your message about doing it in php my admin earlier but im getting ready for work, figured i'd do it when i got home Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744657 Share on other sites More sharing options...
Maq Posted January 23, 2009 Share Posted January 23, 2009 I don't understand this: if ($num_rows > 4){ for ($counter=10; $num_rows > 4; $counter++){ I mean I understand it but I don't get the point of it. How is $num_rows being assigned? Cause if it's running some kind of infinite loop it will delete everything since you have MAX(date). Every time you delete a column the next MAX(date) will be deleted. Add the echo statement in here: $todelete = "DELETE FROM newitems WHERE date = '{$delete['date']}'"; echo $sql . " "; Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-744662 Share on other sites More sharing options...
mike12255 Posted January 24, 2009 Author Share Posted January 24, 2009 Ok, well i know a guy that is an amazing programmer and he helped me rewrite that code in two lines and save my database space: $newitems = mysql_query("SELECT * FROM forumtutorial_posts ORDER BY realtime DESC LIMIT 4"); // for the newest posts. while($row = mysql_fetch_array($newitems)) { print($row["postid"] . " " . $row["author"] . " " . $row["time"]); } Thanks to Precision(my friend - not on this forum) Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-745095 Share on other sites More sharing options...
uniflare Posted January 24, 2009 Share Posted January 24, 2009 yes, its quicker if you use an instant messenger or talk to someone in person. Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-745097 Share on other sites More sharing options...
Maq Posted January 24, 2009 Share Posted January 24, 2009 Ok, well i know a guy that is an amazing programmer and he helped me rewrite that code in two lines and save my database space: $newitems = mysql_query("SELECT * FROM forumtutorial_posts ORDER BY realtime DESC LIMIT 4"); // for the newest posts. while($row = mysql_fetch_array($newitems)) { print($row["postid"] . " " . $row["author"] . " " . $row["time"]); } Thanks to Precision(my friend - not on this forum) Maybe I'm confused but, how does that solve your problem? I thought you needed help deleting not displaying the 4 most recent posts... Quote Link to comment https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/#findComment-745312 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.