caraldur Posted April 10, 2006 Share Posted April 10, 2006 I have a while statement that inserts information into a DB when the page loads. The problem is that for some reason it inserts 2 records instead of just one. I'm not sure what is causing this problem but it's driving me nuts and I need some help.Here is my code:[code] while ($hex = mysql_fetch_array($getHex)) { $hexid1= $hex['Hex_Id']; $hextype1 = $hex['Hex_Type']; $hexNumber1 = $hex['HexNumber']; $hexgranted1 = $hex['Granted']; $hexanimal1 = $hex['animal']; $hexmineral1=$hex['mineral']; $hexvegetable1=$hex['vegetable']; $hexpop=$hex['Population']; $hexdominion1=$hex['Dominion_Id']; $totalhexpop = $totalhexpop + $hexpop; //Total hex that will be added to the Total Dominion Population $month = $row_getHistory['Month']; $year = $row_getHistory['Year']; $totalmonths = 12; ///////////////////////////////////////////////////////// //Advances month by one unless month = 12 then it increases the years by one. if($month == $totalmonths){ $month = 1; $year = ($year +1); }else{ $month = $month + 1; } //////////////////////////////////////////////////////////////// if($hexgranted[$i] ==1){$totalgranted = $totalgranted+1; } $newpop = HexPopCal($hexpop); $hexmonth1=$month; $hexyear1=$year; $hexinsert ="INSERT INTO historyhex (Hex_Id, Dominion_Id, HexNumber, HexType, animal, vegetable, mineral, Population, Month, Year) VALUES ('$hexid1','$hexdominion1', '$hexNumber1', '$hextype1', '$hexanimal1', '$hexvegetable1', '$hexmineral1', '$newpop', '$hexmonth1', '$hexyear1')"; mysql_select_db($database_Norwold, $Norwold); $Result1 = mysql_query($hexinsert, $Norwold) or die(mysql_error()); }[/code]I have searched the forums but I have found nothing that relates to my problem. Hopefully one of the gurus and help me out.Thanks Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/ Share on other sites More sharing options...
wildteen88 Posted April 10, 2006 Share Posted April 10, 2006 Should the variable $getHex only contian one result? If it should then dont use a while loop, otherwise if $getHex has 2 or more results the while loop will loop 2 or 3 more times depending on the number of results $getHex returns from the previous MySQL query. And so you get duplicate results.Strip the while loop out and use this instead:[code]$hex = mysql_fetch_array($getHex);[/code]Also dont for get to change this:[code] $Result1 = mysql_query($hexinsert, $Norwold) or die(mysql_error());}[/code]to:[code] $Result1 = mysql_query($hexinsert, $Norwold) or die(mysql_error());[/code]That way your code will execute just one. Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-25453 Share on other sites More sharing options...
caraldur Posted April 10, 2006 Author Share Posted April 10, 2006 There can be multiple results of $getHex and if there is 2 records for each result are entered into the database. Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-25456 Share on other sites More sharing options...
caraldur Posted April 10, 2006 Author Share Posted April 10, 2006 I really need help with this one, I know there has to be someone out there that can help me with this problem. Please give me a hand.thanks Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-25679 Share on other sites More sharing options...
caraldur Posted April 11, 2006 Author Share Posted April 11, 2006 Help please-Fred Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-25857 Share on other sites More sharing options...
trq Posted April 11, 2006 Share Posted April 11, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]There can be multiple results of $getHex and if there is 2 records for each result are entered into the database.[/quote]Obviously there is more than one result in $getHex. What do you expect?Can we see the query that returns the result $getHex? Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-25865 Share on other sites More sharing options...
caraldur Posted April 11, 2006 Author Share Posted April 11, 2006 Here is the query:[code]$colname_getHex ="Norwold"; if (isset($_GET['id'])) { $colname_getHex = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']); } mysql_select_db($database_Norwold, $Norwold); $query_getHex = sprintf("SELECT * FROM hex_pool WHERE hex_pool.Dominion_Id = '%s'", $colname_getHex); $getHex = mysql_query($query_getHex, $Norwold) or die(mysql_error()); //$row_getHex = mysql_fetch_assoc($getHex); $totalRows_getHex = mysql_num_rows($getHex); [/code]The problem is that I'm getting 2 identical inserts for one result for the above query. I understand there will be multiple inserts for multiple results from my query and that is what I want but 2 indential inserts forever result isn't. Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-25870 Share on other sites More sharing options...
trq Posted April 11, 2006 Share Posted April 11, 2006 The only way this could be happening is your while() is looping twice, and this could only happen if you have two results.What does $totalRows_getHex give you? Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-25872 Share on other sites More sharing options...
caraldur Posted April 11, 2006 Author Share Posted April 11, 2006 $totalRows_getHex is returning a value of 1This problem is has been driving me nuts because it should only loop once because one 1 result is being returned. Is there a better way to write this code so that it will work properly?-Fred Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-25876 Share on other sites More sharing options...
caraldur Posted April 12, 2006 Author Share Posted April 12, 2006 Bump Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-26069 Share on other sites More sharing options...
caraldur Posted April 12, 2006 Author Share Posted April 12, 2006 I hate to keep bumping this up but I really need some help with this problem.-Fred Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-26144 Share on other sites More sharing options...
caraldur Posted April 12, 2006 Author Share Posted April 12, 2006 bump again Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-26291 Share on other sites More sharing options...
caraldur Posted April 13, 2006 Author Share Posted April 13, 2006 Bump Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-26567 Share on other sites More sharing options...
caraldur Posted April 13, 2006 Author Share Posted April 13, 2006 Come on someone has to know how to fix this problem. I really need some help with it. Quote Link to comment https://forums.phpfreaks.com/topic/7009-duplicate-record-insert/#findComment-26687 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.