Walshie1987
Walshie1987

Reputation: 443

MySQL / PHP Not inserting correct amount of entries

Hopefully this will be quite simple for someone.

I have the following code:

    <?php
 // Connects to your Database 
 mysql_connect("localhost", "xxxxx", "xxxxx") or die(mysql_error()); 
 mysql_select_db("xxxxx") or die(mysql_error()); 

  require("../includes/common.php");
  require("admin_header.php");



  require("admin_menu.php");
 $query = "Truncate TABLE pt_menutitles"; 

$result = mysql_query($query) or die(mysql_error());


// Menu Headers for Category

$data = "select a.menuheader, a.total from(SELECT distinct (menuheader),count(*) as total FROM `pt_products` WHERE `menuheader` <> ''  group by `menuheader` order by total desc limit 4) a order by a.menuheader"; 

$result = mysql_query($data) or die(mysql_error());


while($info = mysql_fetch_array($result)) 
  {

 $menudata = "select a.subcategory, a.menuheader,a.totcount FROM(SELECT distinct (subcategory),menuheader,count(*) as totcount FROM `pt_products` WHERE `menuheader`='".$info['menuheader']."' AND subcategory <> ''group by `subcategory` order by totcount desc limit 4) a order by a.subcategory"; 


$menuresult = mysql_query($menudata) or die(mysql_error());


 while($menuinfo = mysql_fetch_array($menuresult)) 

 { 


      $sql = "Insert into pt_menutitles (menu, title, totalcount) select '".$menuinfo['menuheader']."','".$menuinfo ['subcategory']."','".$menuinfo ['totcount']."'";

      $result = mysql_query($sql) or die(mysql_error());



  }

  }

  ?>

Basically I take the top 4 menu titles that have the most items in them, then select the top 4 subcategories in them titles and insert them into a table.

What is happening though is that i'm onlyt getting Menu Title 1 and Subcategories 1 to 4 inserted into my table.

It's as though the the loop is ending after the first time round?

Any advise would be great!

Cheers Chris

Upvotes: 0

Views: 62

Answers (1)

Martin
Martin

Reputation: 1213

This line replaces your existing result and ends the loop. Use any other (non-existing) variable name, but not $result.

$sql = "Insert into pt_menutitles (menu, title, totalcount) select '".$menuinfo['menuheader']."','".$menuinfo ['subcategory']."','".$menuinfo ['totcount']."'";
$result = mysql_query($sql) or die(mysql_error());

Upvotes: 1

Related Questions