Jump to content

Duplicate Record insert


caraldur

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
Link to comment
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.
Link to comment
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?
Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.