SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DB entries duplicating

    Hi

    I have set up a report system for a client who can add text report relating to various products. Any one report may relate to a number of products so I am using a lookup table to join the reports and the products together.

    I have done this in the past without error on other sites but on this occassion, whenever I enter the data into the lookup table I get a duplication of the last entry

    eg. I have a report that relates to products 1,3 (selected from a checkbox list) and I am using the foreach command to enter a lookup entry for them both.
    Code:
        
        	if($products =="") $products = array();
       
       foreach ($products as $productID) {
       	
       	
       	$sql = "INSERT IGNORE INTO ph_ReportLookup
       			SET ReportID=$reportID, ProductID=$productID";
       	$ok = @mysql_query($sql);
       	if ($ok) {
       	echo ("report-".$reportID." Product ID-".$productID."<br />");
       	}else{
       	echo("<p>Error inserting report into product" . "$productID: " . mysql_error() . "</p>");
       	}
       }
    I have put the echo statement in to try and see what is happening and it shows everything is OK. It displays once for each product.

    But when I check the lookup table, the report will have an additional enty - a duplication of the last one.

    In my example, the lookup table would end up like:

    Code:
      ReportID             ProductID
       1 			  1
       1			  3
       1			  3
    This is driving me nuts - can anyone see what I am doing wrong?

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you say the loop gets executed twice (with that sample data) but you get three entries?
    I would guess that somewhere you use the $sql SQL query string again without setting it to something new (some if-else case that didn't catch every possibility/error).

    Could this be, check the code following this part, please.

  3. #3
    SitePoint Evangelist elgumbo's Avatar
    Join Date
    Nov 2002
    Location
    North West, UK
    Posts
    545
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks for your help.

    I was using the $sql statement again to check if the query had been run but I'd messed it up.

    Thanks for helping me focus


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •