Jump to content


Photo

Duplicate Record insert


  • Please log in to reply
13 replies to this topic

#1 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 10 April 2006 - 10:25 AM

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:

 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()); 
 }

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


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 10 April 2006 - 10:34 AM

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:
$hex = mysql_fetch_array($getHex);
Also dont for get to change this:
   $Result1 = mysql_query($hexinsert, $Norwold) or die(mysql_error());
}
to:
   $Result1 = mysql_query($hexinsert, $Norwold) or die(mysql_error());
That way your code will execute just one.

#3 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 10 April 2006 - 10:41 AM

There can be multiple results of $getHex and if there is 2 records for each result are entered into the database.



#4 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 10 April 2006 - 10:57 PM

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



#5 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 11 April 2006 - 02:11 PM

Help please

-Fred

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 April 2006 - 02:30 PM

[!--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?

#7 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 11 April 2006 - 02:37 PM

Here is the query:
$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); 

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.



#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 April 2006 - 02:45 PM

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?

#9 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 11 April 2006 - 02:50 PM

$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

#10 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 12 April 2006 - 02:46 AM

Bump

#11 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 12 April 2006 - 09:49 AM

I hate to keep bumping this up but I really need some help with this problem.

-Fred

#12 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 12 April 2006 - 04:42 PM

bump again

#13 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 April 2006 - 01:05 PM

Bump

#14 caraldur

caraldur
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 April 2006 - 06:00 PM

Come on someone has to know how to fix this problem. I really need some help with it.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users