Page 1 of 1

an hidden infinite loop

Posted: Wed May 25, 2005 5:38 pm
by matthiasone
There appears to be an infinite loop problem with this code, but I can not find it. Here is the code

Code: Select all

function order_complete()
{
  session_start();
  chck_nomaster_list(); 
  if( isset($_SESSION['client']['cid']))
  {
    $query = "INSERT INTO orders VALUES ('".$_SESSION['order']['rid']."', '".
	         $_SESSION['order']['odate']."', '".$_SESSION['client']['cid']."', '".
			 $_SESSION['order']['otype']."', '".$_SESSION['order']['rep']."', '".
		     ($_SESSION['acount'] + $_SESSION['vcount'])."', '', '".$_SESSION['order']['comments']."', '".
			 $_SESSION['shipping']."', '".$_SESSION['handling']."', 'N', 'N', 'N', 'N','N')";
      echo 'oq='.$query.'<br>';
    $oresult = query_db($query, '2');
    if (mysql_affected_rows() == 1)
      $oflag = 1;
    else
      $oflag = 0;	
    echo 'of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
    echo 'dc='.$_SESSION['cnt'].'<br>';
	for($i = 0; $i <= $_SESSION['cnt']; $i++)
    {
      if (isset($_SESSION['oitem'][$i]['media']))
	  { 
        $cnt ++;
	    $oiid = makeoiid($cnt);
        $query = "INSERT INTO orderdetails VALUES ('".$oiid."', '".$_SESSION['order']['rid']."', '".
                 $_SESSION['oitem'][$i]['sid']."', '".$_SESSION['oitem'][$i]['qty']."', '".
				 $_SESSION['oitem'][$i]['media']."', '".$_SESSION['oitem'][$i]['uprice'].
				 "', 'N', 'N', '0000-00-00', 'N', 'N', 'N')";
      echo 'oiq='.$query.'<br>';
	    $oiresult = query_db($query, '2');
        if (mysql_affected_rows() >= 1)
          $oiflag = 1;
        else
          $oiflag = 0;	
	  }
    }
    $cnt = 0;	  
    echo 'of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
    for($p = 0; $p <= count($_SESSION['package'])-1; $p++)
    {
      $cnt ++;
	  $pid = make_pid($cnt);
      $query = "INSERT INTO orderpackages VALUES ('".$pid."', '".$_SESSION['order']['rid']."', '".
               ($_SESSION['acount'] + $_SESSION['vcount'])."', '".$_SESSION['package'][$p]['pkg']."')";
      echo 'pq='.$query.'<br>';
	  $tresult = query_db($query, '2');
	  if (mysql_affected_rows() >= 1)
        $pflag = 1;
       else
        $pflag = 0;
    }
    $cnt = 0;	  
    echo 'of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
    echo 'tc='.$_SESSION['tcnt'].'<br>';
    for($t = 0; $t <= $_SESSION['tcnt']; $t++)
    {
	  if (isset($_SESSION['tran'][$t]['ttype']))
	  { 
        $cnt ++;
	    $tid = maketid($cnt);
        $query = "INSERT INTO ordertrans VALUES ('".$tid."', '".$_SESSION['order']['rid']."', '".
                 $_SESSION['tran'][$t]['ptype']."', '".$_SESSION['tran'][$t]['ttype']."', '".
			     $_SESSION['tran'][$t]['cknum']."', '".$_SESSION['tran'][$t]['amt']."', 'N')";
        echo 'tq='.$query.'<br>';
	    $tresult = query_db($query, '2');
	    if (mysql_affected_rows() >= 1)
          $tflag = 1;
        else
          $tflag = 0;
       }
    }
    echo 'of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
	if($oflag == 1 and $oiflag == 1 and $tflag == 1 and $pflag == 1)
    {  
  	  $rid = $_SESSION['order']['rid'];
      $user = $_SESSION['user'];
      $repnum = $_SESSION['repnum'];
      $tech = $_SESSION['tech'];
	  session_unset();
	  $_SESSION['user'] = $user;
      $_SESSION['repnum'] = $repnum;
      $_SESSION['tech'] = $tech;
    }
  }		
    echo 'final of='.$oflag.'oif='.$oiflag.'pf='.$pflag.'tf='.$tflag.'<br>';
	order_complete_form($rid);
}
Any help would be appreciated[/quote]

Posted: Wed May 25, 2005 5:42 pm
by shiznatix
for($i = 0; $i <= $_SESSION['cnt']; $i++)

what is $_SESSION['cnt'] and $_SESSION['tcnt']

Posted: Wed May 25, 2005 5:47 pm
by matthiasone
They actually are showing the correct information.

Posted: Wed May 25, 2005 5:49 pm
by shiznatix
maybe its happening in one of the many other fuctions you are running inside that function

Posted: Wed May 25, 2005 5:51 pm
by thomas777neo
My best advice is to comment out all the loops except one, then execute the code. Do this until you have found the loop hole.

Also try and have a breaking mechanism in a loop. So if for example, you know the loop will return the number of records in a table. If the counter + 1 > number of rows, break the loop and echo something.

Just take it one step at a time, I feel that some of the values that you store in the session could be the problem.

Enjoy debugging, your loops look structurally correct.

Posted: Wed May 25, 2005 5:53 pm
by thomas777neo
I agree with shiznatix, check your other functions out first