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

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” ?

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

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.

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

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

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.

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

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