case "deletearticle":
$result = mysql_query("SELECT id FROM articles WHERE id = '$aid'");
$count=mysql_num_rows($result);
if($count == '1') {
mysql_query("DELETE FROM articles WHERE id='$aid'") or die(mysql_error());
?>
Article deleted
<?
} else {
?>
Article not found
<?
die;
}
break; 

When testing this I posted about 100 test "articles" and tried to delete them one by one. I deleted 8 articles. On 9th article I got "Article not found" (but the article was successfully deleted). Then I deleted 18 articles and on next article again "Article not found". Sometimes it happends often. When I look at log files I see double page calls with the same values. For example: http://www/article.php?op=delete&id=19. I am sure I do not click twice on "delete".

What can cause situations that the same script occasionally executes twice? By the way before error I don't see that page refreshes. I just click on "Delete" and the output is "Article not found". How it's possible that query runs twice?

    Well that's certainly a mess of code. Try rewriting it more... sensibly.. like this:

    case 'deletearticle':
    	$query = "DELETE FROM `articles` WHERE `id` = $aid";
    	$exec = mysql_query($query) or die('MySQL error: ' . mysql_error());
    
    if(mysql_affected_rows($exec) > 0)
    	echo 'Article deleted';
    else
    	echo 'Article not found';
    
    die;
    break;

      What can cause situations that the same script occasionally executes twice? By the way before error I don't see that page refreshes. I just click on "Delete" and the output is "Article not found". How it's possible that query runs twice?

      what is very important
      is when you make a redirect in a php script
      is that you put an exit; ( same as die; )

      if we dont, there can be strange things happening
      as the php script might continue to run after the redirect

      Does not matter if you do it by header location
      or provide a link to go to next page

      <?php
      
      if ( $goelsewhere ) {
      
      header ('location: elsewhere.php')
      exit;
      
      }
      
      ////////////////////
      
      if ( $goelsewhere ) {
      
      echo '<a href="elsewhere.php">Go somewhere else</a>';
      exit;
      
      }
      

        halojoy thanks again. Yes, I tried with "exit" but it doesn't help.

        The file is designed in this way:

        <?php
        include_once 'funkcions.inc.php';
        include_once 'language.php';
        include_once 'header.php';
        opentable();
        
        switch($do) {
        
        default:
        .....................
        break;
        
        case "showarticles":
        ......................
        break;
        
        case "deletearticle":
        .......................
        break;
        
        }
        
        closetable();
        include_once 'footer.php';
        ?>
        

        So, I don't understand how it can continue to run because at the end of "case" it closes table and includes footer.php. That's all - nothing to run. How can it execute twice? As I wrote in my other thread about redirecting, the error happends even if I remove any "redirects" from the script.

        bradgrafelman, I can rewrite it more sensibly. Maybe that's the problem because I don't see this error in scripts which are written in this way:

        $result=mysql_query(...

          2005 wrote:

          When I look at log files I see double page calls with the same values.

          Well it can hardly be the fault of the script if it runs twice because it's been requested twice. It sounds like you're trying to do it manually, rather than writing a program to make the requests; how rapidly are you making delete requests and are you sure you're not just losing count or tripping over yourself?

            23 days later

            I did as suggested by bradgrafelman and there are no errors anymore.

              Write a Reply...