function not seeing variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

function not seeing variables

Post by tristanlee85 »

Code: Select all

// BEGIN DISCOUNT COUPON CODE //
	 // Database information
        $store_host = "localhost";
        $store_user = "user";
        $store_pass = "pass";
        $store_db = "database";
        $store_table = "xcart_discount_coupons";
        $discount_value = "10.00";
        $coupon_status = false;
	
	// We're setting a value for $coupon_code //
	if (isset($HTTP_POST_VARS['coupon']) && $HTTP_POST_VARS['coupon'] != "")
	{
		$coupon_code = $HTTP_POST_VARS['coupon'];
	}
	else if (isset($HTTP_GET_VARS['coupon']) && $HTTP_GET_VARS['coupon'] != "")
	{
		$coupon_code = $HTTP_GET_VARS['coupon'];
	}
	else
	{
		$coupon_code = "";
	}

	// Decide if we can display the coupon code in the registration page //
	if ($_SERVER["HTTP_REFERER"] == "http://forums.plastikracing.net/discount_register.php" && isset($HTTP_GET_VARS['coupon']))
	{
		$discount_code = "<tr>
                        <td class=\"row1\"><span class=\"explaintitle\">Discount Coupon:</span><br />
                        <span class=\"gensmall\">Your discount coupon for the on-line store.</span></td>
                        <td class=\"row2\">
                        <input type=\"text\" name=\"coupon\" value=\"" . $coupon_code . "\" maxlength=\"14\" class=\"post\" />
                        </td>
                	</tr>";
	}

	// Add the coupon to the store //
	 function createCoupon ()
        {
                @mysql_connect("localhost","user","pass") or die(mysql_error());
                @mysql_select_db("database") or die(mysql_error());

                $coupon_sql = "INSERT INTO ".$store_table." VALUES ('".$coupon_code."', '".$discount_value."', 'absolute', '0', '0', '10.00', '1', 'N', '0', 					'1230699600', 'A', 'master', 'N', 'N', 'N')";
                mysql_query($coupon_sql) or die('Query failed: ' . mysql_error());
                mysql_close();
		
		$coupon_status = true;
        }
// END DISCOUNT COUPON CODE //
The problem I am having is $coupon_code and $discount_value don't have any value inside the function. I figured maybe it was because $coupon_code wasn't being posted to the page, but even $discount_value that's already defined doesn't have a value either. I'm not sure what is wrong here.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Number one the function is never called.
Number two those variables you want to be defined are not in the variable scope.

http://php.net/variables.scope
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

The function is called later in the script. So I guess either make them global inside the function or pass them to the function.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Pass them to the function. "globalising" variables is considered very bad practice. Some other points to note.

Use $_GET instead of $HTTP_GET_VARS and $_POST instead of $HTTP_POST_VARS.

Read up on mysql_real_escape_string if you want prevent sql injection attacks... HINT... You very much want to prevent this :wink:
Post Reply