SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    AdCaliber Magic2K2's Avatar
    Join Date
    Nov 2001
    Location
    Northern Virginia
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Script repeats itself - why?

    I've had this problem for a while. Sometimes I run an update script and it repeats itself for some reason and inserts everything into the DB twice or more. It makes no sense at all. At the end of the condition, it is supposed to redirect to another page, but it's like it ignores that and randomly goes back and repeats the SQL queries.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can we see the code?

  3. #3
    AdCaliber Magic2K2's Avatar
    Join Date
    Nov 2001
    Location
    Northern Virginia
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, to clarify...I don't see how it can be a coding issue because sometimes it works properly and sometimes it does not. There are a lot of SQL queries for it to execute so could this be some type of timeout?

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it might not be a syntax error BUt it can be alogic error. unless you post your code, none can see what's going on and no one can try to fix it.
    ---------------------------
    Errors = Improved Programming.
    My Site

  5. #5
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe the user is clicking the submit button twice. That happens all the time. An easy workaround is to disable the submit button using javascript when it's clicked using the onclick event. If you disable it on a timer for, say, 10 seconds, it would give the user the opportunity to re-submit the data if there is an error, but stop them from clicking twice too quickly.

  6. #6
    AdCaliber Magic2K2's Avatar
    Join Date
    Nov 2001
    Location
    Northern Virginia
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not the submit button being pressed twice because it's an administrative tool that only I use! Here is the code with certain things edited out for security:

    PHP Code:
    else if ($step == 3)
    {
        
    $sql str_replace("\\"""$sql);
        
    $commands explode(";"$sql);
        for (
    $x 0$commands[$x]; $x++)
            
    mysql_query($commands[$x]);

    //// LARGE PORTION EDITED OUT

        
    redirect();


    I edited out a large portion, but the mysql_query you see above is where the problem occurs. $sql is valid sql passed from the previous page. What happens is I will run the script and I'll notice that it executed all the SQL commands twice or more. So, I delete those rows in the DB manually and then run the exact same script again and BINGO it works. Today I decided to post about it because it did the double insert thing like 10 straight times and was getting on my nerves, but on the 11th time it worked properly.

  7. #7
    SitePoint Addict
    Join Date
    Feb 2005
    Location
    Maine
    Posts
    244
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all, you can still press the submit button twice. You aren't God.

    Second of all, just post the whole script... It will show up with a scrollbar anyway, it won't take up tons of space or anything.


  8. #8
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    PHP Code:
    for ($x 0$commands[$x]; $x++) 
    You have to make the middle expression evaluate as FALSE at some point in order to break the loop. That means it HAS to be an expression, and one that evaluates false or you'll be stuck!

    I believe you must be looking for replacing:
    PHP Code:
    $commands[$x
    with:
    PHP Code:
    $x sizeof($commands
    This will evaluate as false when $x is the size of the $commands array, meaning the last command was executed!
    For more information: http://php.net/for
    Last edited by DMacedo; Mar 30, 2005 at 17:24. Reason: wooops ;)
    ~ Daniel Macedo

  9. #9
    AdCaliber Magic2K2's Avatar
    Join Date
    Nov 2001
    Location
    Northern Virginia
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks DMacedo, I'll do that. I'm still curious why the script works sometimes and sometimes does not, though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •