Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/142149-solved-cant-get-delete-to-work/
Share on other sites

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

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

<?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']}'";

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

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

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)

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

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.