Problem comparing variables

Discussion in 'PHP' started by frobak, Nov 5, 2010.

  1. #1
    Hi I am trying to compare 2 variables, but am having a problem.

    I am using a mysql database to log promotion codes. There is a limit on the amount of times a paticular promotion code can be used.

    I have a promotion code table, holding the promotion codes and the amount of times it can be used. I then have a promotion code log, which is logging the amount of times the promotion code has been used.

    I want to compare the amount of times a promo code can be used to the number of rows returned from the promo code log.

    In theory really easy but I am having a problem.

    If i use the following code:

    if ($use_limit >= $num) 
    PHP:
    it should run if the the limit is greater than or equeal to the amount of rows, but it doesnt, it returns false!

    If i use the following code:

    if ($use_limit == $num) 
    PHP:
    and change the rows in the database to match the limit it works.

    So its something to do with the comparison operator i guess ($use_limit >= $num)

    Am i doing somethign wrong here?

    cheers

    here is the entire code:
    		else {
    		
    			$query = "SELECT * FROM multi_promo WHERE promo_code = '$promo_code'";
    			
    			$result = mysql_query($query);
    			$num = mysql_num_rows($result);
    			
    			if ($num == '1') {	
    			
    				$sql = "SELECT * FROM multi_promo WHERE promo_code = '$promo_code'";
    			
    				$result = @mysql_query($sql,$connection) 
    					or die(mysql_error());
    					
    					while ($row = mysql_fetch_array($result)) {
    						$promo_code		= $row['promo_code'];
    						$valid_from	 	= $row['valid_from'];
    						$valid_to		= $row['valid_to'];
    						$use_limit	 	= $row['use_limit'];
    					}
    			
    				$sql = "SELECT promo_code FROM multi_promo_log WHERE promo_code = '$promo_code'";
    				$result=mysql_query($sql);
    				$num=mysql_num_rows($result);
    			
    				if ($use_limit >= $num) {
    				
    				$sql = "INSERT INTO multi_promo_log SET
    				promo_code = '$promo_code'";
    					
    				$result = @mysql_query($sql,$connection) or die(mysql_error());
    				
    				$sql = "INSERT INTO transaction SET
    				name  			= '$_POST[name]',
    				email			= '$_POST[email]',
    				contact 		= '$_POST[contact]', 
    				serv_lgth		= '$_POST[serv_lgth]',
    				contract		= '$_POST[contract]', 			
    				dl_url		    = '$uploadpath$filename', 
    				item_no			= '$item_no',
    				product			= '$product',
    				price			= '$price',
    				date			= 'CURDATE()',
    				quest			= '$_POST[quest]'";
    					
    				$result = @mysql_query($sql,$connection) or die(mysql_error());
    				$invoice_no = mysql_insert_id();
    				
    				header("Location: promo_test.php?invoice_no=$invoice_no&name=$_POST[name]&email=$_POST[email]&contact=$_POST[contact]&serv_lgth=$_POST[serv_lgth]&contract=$_POST[contract]&st=Completed");
    				}
    			else
    				{
    				header("Location: ../promo_error.php?error=This promo has been used more than the limit");
    				}
    		}
    		else {header("Location: ../promo_error.php?error=This promo has less than one line in the db");}
    	}
    PHP:
     
    Last edited: Nov 5, 2010
    frobak, Nov 5, 2010 IP
  2. Walk

    Walk Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Variable scope. You need to declare the use_limit variable BEFORE you enter any conditional statements, loops or functions.
     
    Walk, Nov 5, 2010 IP