Jump to content

[SOLVED] Why this foreach is running twice ?


Gurzi

Recommended Posts

I did var_dump at the $_POST and it has returned :

 

array(2) { ["idProduto"]=>  string(1) "2" ["produtos"]=>  array(1) { [0]=>  string(1) "1" } }

 

 

after i did this

foreach($_POST['produtos'] AS $key=>$value){
    $adicionaProdutoQuery = "INSERT INTO Proposta_Produto_Temporario(IDPropostaTemporaria,IDProduto)
							 VALUES($idProposta,$value)"; 

//echo $adicionaProdutoQuery . "<br />";
					$database->execute($adicionaProdutoQuery);	


}

The funniest thing here, is that if i use the echo, it just prints one time, but if i use the $database->execute() , it inserts the query twice. I really don't understand :x

 

Any ideas ?

 

 

That will be impossible. If your output inside the foreach is only one time, meaning the block code inside the foreach will only executed one time.

 

Try use constant string for output like:

echo "a";

inside the foreach to make sure is only running one time.

 

Beside, please check your database class's function, 'execute'.

That will be impossible. If your output inside the foreach is only one time, meaning the block code inside the foreach will only executed one time.

 

Try use constant string for output like:

echo "a";

inside the foreach to make sure is only running one time.

 

Beside, please check your database class's function, 'execute'.

 

I said the same, it's impossible, but it's happening !! I changed $database->execute to mysql_query() and it's the same, it inserts the query twice and i tried using echo constant string has you said and it just returned one time.

 

The stupid thing now it's that i have this

 

foreach($_POST['produtos'] AS $key=>$value){

 

 

$adicionaProdutoQuery = "INSERT INTO Proposta_Produto_Temporario(IDPropostaTemporaria,IDProduto)

VALUES($idProposta,$value)"; 

 

//echo $adicionaProdutoQuery . "<br />";

//$database->execute($adicionaProdutoQuery);

mysql_query($adicionaProdutoQuery);

echo "a";

 

}

 

and it's executing the query 2 times but i just see a single "a" character.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.