Jump to content

Archived

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

caraldur

Duplicate Record insert

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
There can be multiple results of $getHex and if there is 2 records for each result are entered into the database.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[!--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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
$totalRows_getHex is returning a value of 1

This 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

Share this post


Link to post
Share on other sites

×

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.