Gurzi Posted May 10, 2008 Share Posted May 10, 2008 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/105023-solved-why-this-foreach-is-running-twice/ Share on other sites More sharing options...
xenophobia Posted May 10, 2008 Share Posted May 10, 2008 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'. Quote Link to comment https://forums.phpfreaks.com/topic/105023-solved-why-this-foreach-is-running-twice/#findComment-537575 Share on other sites More sharing options...
Gurzi Posted May 10, 2008 Author Share Posted May 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/105023-solved-why-this-foreach-is-running-twice/#findComment-537591 Share on other sites More sharing options...
Gurzi Posted May 10, 2008 Author Share Posted May 10, 2008 FOUND! Sorry but i'm working on a team and i am using ajax, and the another guy forgot to retire the name of the file from the <form action=""> so it was calling the file twice I didn't see it Sorry And thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/105023-solved-why-this-foreach-is-running-twice/#findComment-537596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.