SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    INSERTS ARRAY DATA TWICE ERROR

    I'm not sure whether I've posted in the right place. It's either here or the php forum.

    I'm just messing around with adding data to a database and have the following code

    PHP Code:
    <?php 
    include '../includes/newdbconn.php';
    $result= array("You SHALL be a php stallion of the information superhighway");
    foreach(
    $result as $k=>$v):
    try{
    $sql 'INSERT INTO response SET
                answer = :answer'
    ;
    $s $connect->prepare($sql);
    $s->bindValue(':answer'$v);
    $s->execute();
    }
    catch (
    PDOException $e) {
        
    $output='Error inserting into database. ';
        
    $output.= $e->getMessage();
        include 
    '../includes/output.inc.php';
        exit();
        }
    endforeach;
        
    //print_r($result);
    ?>
    The problem is the data gets inserted TWICE. Duplicate entries of everything.
    What's going on? It must be really obvious but I've searched for about 4 hours and returned empty handed.
    Can anyone help please.

    EM
    Last edited by cpradio; Jun 20, 2013 at 08:02. Reason: Added php tags

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I've moved it to the PHP forum.

    This is the exact code that is creating the duplicate entries?
    If you empty the response table completely, and run the script once, you've got two rows with "You SHALL be a php stallion of the information superhighway" ?

  3. #3
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes exactly. If i empty the database and run it then i get two entries of each.

    I should also say that the duplicate entries are not together in the database.
    What i mean is, if i had say 4 entities in the array then the database would show the those 4 entries then repeat them - so 8 entries in total. It's as if it's going through the array twice.

    EM

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you included the file you pasted in another file twice. Do a search for where that file is used in include() or require() and see if it occurs more than once.

  5. #5
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cpradio you've done it again!

    It was the include statement. I changed the database connection request to 'require_once' instead of 'include' and it works like a charm.

    But why does including the file make it run through the array twice???

    EM

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Beecause you are including the file multiple times. PHP sees the file as a new file each time it is included, so it doesn't realize it already processed the file, so it processes it again from start to finish. Using include_once or require_once allows you to tell PHP to throw a warning/error if the file is attempted to be included multiple times (which is why your issue was resolved).

  7. #7
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I get you.
    So when it reads the line...
    "include '../includes/newdbconn.php';" it goes off and gets the connection file.
    THEN it comes back and starts at the top of the script and reads the include statement AGAIN and does the same thing.
    So is it making two connections and inserting data through each one???

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by emeticman View Post
    I think I get you.
    So when it reads the line...
    "include '../includes/newdbconn.php';" it goes off and gets the connection file.
    THEN it comes back and starts at the top of the script and reads the include statement AGAIN and does the same thing.
    So is it making two connections and inserting data through each one???
    I wouldn't think that would be happening, but more so the file your code was in was being included twice. so it was building a connection, running the array/insert logic, then exiting, then the second included file was building a connection, running the array/insert logic, then existing.

  9. #9
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a bit embarrassed to continue this thread because i said yesterday it was sorted - but unfortunately it's not.
    When i said it was fixed i was looking at the response table in phpMyAdmin to see what was entered but i did not click the 'Show All' option at the bottom of the table which would have shown again the duplicate entries, (the script on my local machine has over 20 entities in the array).

    On a plus note I think I've solved it but still have absolutely no idea why it works.
    Here's the new code:
    PHP Code:
    <?php 
    include '../includes/newdbconn.php';
    $result= array("You SHALL be a php stallion of the information superhighway");
    foreach(
    $result as $k=>$v):
    try{
    $sql 'INSERT INTO response SET
                answer = :answer'
    ;
    $s $connect->prepare($sql);
    $s->bindValue(':answer'$v);
    $s->execute();
    }
    catch (
    PDOException $e) {
        
    $output='Error inserting into database. ';
        
    $output.= $e->getMessage();
        include 
    '../includes/output.inc.php';
        exit();
        }
    endforeach;
        
    //print_r($result);

    Note the ONLY difference is the lack of a closing php tag. There is no other change. But it works (and I've clicked 'show all'). I've emptied the database and reinserted the closing php tag and reloaded only to again get duplicate entries.
    I found the solution on a Zend site which didn't explain it but said that best coding practice for files containing solely php is to leave out the closing tag. Something to do with server headers.
    Can someone explain in terms I would understand why this is happening. Is it common practice to leave out the tag???

    EM
    Last edited by cpradio; Jun 21, 2013 at 06:46. Reason: Added php tags

  10. #10
    SitePoint Member
    Join Date
    Jun 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ignore that last post. It's started happening again. Don't know what's going on. I think it must be something to do with my WAMP set up.

    EM


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
  •