Web Analytics Made Easy -
StatCounter inserts 2 rows automatically - CodingForum

Announcement

Collapse
No announcement yet.

inserts 2 rows automatically

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • inserts 2 rows automatically

    Hey all,

    I've recently run into a "bug". I have a script that, among other things, inserts a row into my MySQL database using PHP.

    9/10 times it either inserts 2 or 3 rows. I couldn't figure it out, and I searched the web hoping that someone else had run into this problem. Sure enough, here's what I came up with:
    http://bugs.php.net/bug.php?id=22363

    Now if you check out one of the links that's supposed to "explain the problem", it refers to some bullcrap about bgcolor. Now I don't have this bgcolor stuff going on in my code. But I also read in that webpage that it may have something to do with broken html and that's what's causing the page to load more than once.

    I still don't know what the problem is with my code (And there's too much of it to post here...), but I don't see any broken html.

    I even inserted some javascript to popup an alert when it was attempting to do the insert, hoping that it would popup more than once if it was in fact reloading the page. Well, it only popped up once, and I had 3 new rows inserted!!

    So my temporary solution is to create a second query, and check to see that my previous insert hadn't gone through. In effect I'm doing a select previous to my insert.

    I don't know what I'm trying to say here, but at the very least, if you do run into this problem, here's a quick fix for it. Hopefully it's applicable for your situation.

    Cheers,
    Sadiq.

  • #2
    Well, if you would post the SQL query and the PHP code that sends this query to your database, we might figure out what the problem actually is. Sounds as if the code to insert the row gets called more than once in the script itself.

    What you have now looks like a crude hack, not a real solution. A slightly better hack would be to use a static variable to set a status flag, that indicates if the insert-function has been called already and if so, returns.
    De gustibus non est disputandum.

    Comment


    • #3
      I haven't used any static stuff in PHP before, so I'd have to research that a bit. But your solution definately sounds better (if it works!). So thanks for that.

      As far as my query and PHP code goes, if you'd really like to see it here it is!

      PHP Code:
      //connect to the database initially so we don't have to keep on doing it
      $db mysql_connect("localhost");
      mysql_select_db("test_alpine_db"$db);
       
      //see what's needed to be done (add, update, delete)
      if(isset($_POST["todo"])) {
       if(
      strcmp($_POST["todo"], "add") == 0) {
        
      $query  "insert into appraisal_firms_table (";
        
      $query .= "appraisal_firm_code,name,telephone,fax,email,unit_number,";
        
      $query .= "street_number,street_name,street_type,street_direction,city,";
        
      $query .= "province,postal_code,po_box_number,rural_route,site,compartment,";
        
      $query .= "areas_covered,rating,comments) values (";
        
      $query .= "NULL,"."\"".$_POST["name"]."\",\"".$_POST["telephone"]."\",\"";
        
      $query .= $_POST["fax"]."\",\"".$_POST["email"]."\",\"".$_POST["unit_number"]."\",\"";
        
      $query .= $_POST["street_number"]."\",\"".$_POST["street_name"]."\",\"".$_POST["street_type"]."\",\"";
        
      $query .= $_POST["street_direction"]."\",\"".$_POST["city"]."\",\"".$_POST["province"]."\",\"";
        
      $query .= $_POST["postal_code"]."\",\"".$_POST["po_box_number"]."\",\"".$_POST["rural_route"]."\",\"";
        
      $query .= $_POST["site"]."\",\"".$_POST["compartment"]."\",\"".$_POST["areas_covered"]."\",\"";
        
      $query .= $_POST["rating"]."\",\"".$_POST["comments"]."\")";

        
      $dbl_check_query "select * from appraisal_firms_table where ";
        
      $dbl_check_query .= "name=\"".$_POST["name"]."\" AND ";
        
      $dbl_check_query .= "telephone=\"".$_POST["telephone"]."\" AND ";
        
      $dbl_check_query .= "fax=\"".$_POST["fax"]."\" AND ";
        
      $dbl_check_query .= "email=\"".$_POST["email"]."\" AND ";
        
      $dbl_check_query .= "unit_number=\"".$_POST["unit_number"]."\" AND ";
        
      $dbl_check_query .= "street_number=\"".$_POST["street_number"]."\" AND ";
        
      $dbl_check_query .= "street_name=\"".$_POST["street_name"]."\" AND ";
        
      $dbl_check_query .= "street_type=\"".$_POST["street_type"]."\" AND ";
        
      $dbl_check_query .= "street_direction=\"".$_POST["street_direction"]."\" AND ";
        
      $dbl_check_query .= "city=\"".$_POST["city"]."\" AND ";
        
      $dbl_check_query .= "province=\"".$_POST["province"]."\" AND ";
        
      $dbl_check_query .= "postal_code=\"".$_POST["postal_code"]."\" AND ";
        
      $dbl_check_query .= "po_box_number=\"".$_POST["po_box_number"]."\" AND ";
        
      $dbl_check_query .= "rural_route=\"".$_POST["rural_route"]."\" AND ";
        
      $dbl_check_query .= "site=\"".$_POST["site"]."\" AND ";
        
      $dbl_check_query .= "compartment=\"".$_POST["compartment"]."\" AND ";
        
      $dbl_check_query .= "areas_covered=\"".$_POST["areas_covered"]."\" AND ";
        
      $dbl_check_query .= "rating=\"".$_POST["rating"]."\" AND ";
        
      $dbl_check_query .= "comments=\"".$_POST["comments"]."\"";
                  
        
      $result mysql_query($dbl_check_query$db) or die("Invalid query ".mysql_error());
        
      $num_rows mysql_num_rows($result);

        if(
      $num_rows <= 0)
         
      $result mysql_query($query$db);
       }

      EDIT: Having a few formatting problems here... but the code does work, so if you intend to copy and paste, just double check your formatting.

      This code does my double-check query hack. If I figure out this static hack, I'll post that one for anyone's future reference.

      Thanks,
      Sadiq.
      Last edited by sad69; Feb 26, 2004, 01:03 PM.

      Comment

      Working...
      X