pozer69 Posted March 16, 2009 Share Posted March 16, 2009 My code doesnt seem to be executing correctly. as i submit on a form the two variables, user and country, are then used to select from the database one of 6 options, all users and all countries, all users and us only, all users and foreign countries, indiv users and all countries, indiv users and us, and indiv users and foreign coutries. It might be a passing variable error, im not sure. I usually code in c++ so my coding may be off. Here is the area that is troubling me: $user=$_POST['user']; $country=$_POST['country']; function indiv_user() { if($country == "us") { $select = "SELECT * FROM companies WHERE user_assigned='$user' AND co_country = 'US'"; $export = mysql_query ( $select ) or die ( "" ); } elseif($country == "foreign") { $select = "SELECT * FROM companies WHERE user_assigned='$user' AND co_country != 'US'"; $export = mysql_query ( $select ) or die ( "" ); } else { $select = "SELECT * FROM companies WHERE user_assigned='$user'"; $export = mysql_query ( $select ) or die ( "" ); } } function all_user() { if($_POST['country']== 'us') { $select = "SELECT * FROM companies WHERE co_country = 'US'"; $export = mysql_query ( $select ) or die ( "" ); } elseif($_POST['country']== 'foreign') { $select = "SELECT * FROM companies WHERE co_country != 'US'"; $export = mysql_query ( $select ) or die ( "" ); } else { $select = "SELECT * FROM companies"; $export = mysql_query ( $select ) or die ( "" ); } } if($user == "all") { all_user(); } else { indiv_user(); } The code below this calls on the export statement but i am receiving an undefined variable export. Everything else on the page works bc if i get rid of the functs and if statements and just have one select and export statement everything works perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/ Share on other sites More sharing options...
JonnoTheDev Posted March 16, 2009 Share Posted March 16, 2009 Your variables are outside the scope of the function. They must be passed in: function indiv_user() { if($country == "us") { function indiv_user($country) { if($country == "us") { Function call indiv_user($_POST['country']); Quote Link to comment https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/#findComment-785837 Share on other sites More sharing options...
pkSML Posted March 16, 2009 Share Posted March 16, 2009 IMO, the easiest way to get variables into your function is to use the global command. <?php $user=$_POST['user']; $country=$_POST['country']; function indiv_user() { global $user, $country; // this is the first line of your function // ...... function all_user() { global $user, $country; // this is the first line of your function ?> Also, make sure your input data in sanitized! You're using raw POST data to enter into your database, which can lead to security issues. See the php manual on mysql_real_escape_string. Quote Link to comment https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/#findComment-785846 Share on other sites More sharing options...
JonnoTheDev Posted March 16, 2009 Share Posted March 16, 2009 IMO, the easiest way to get variables into your function is to use the global command. Never use global variables! This is a cardinal sin and in the top 10 don't dos. Global variables lead to unexpected results when a function call alters variable values outside of its scope. Always pass values into functions whether in a single variable or in an array. Quote Link to comment https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/#findComment-785868 Share on other sites More sharing options...
pozer69 Posted March 16, 2009 Author Share Posted March 16, 2009 thank you for your replies...however i am still receiving an undefined variable export...how do i return/pass the export and select statements from the function? i dont know if i am saying it right but in c/c++ at the end of the function i could throw in a return ($export); Quote Link to comment https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/#findComment-786198 Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 thank you for your replies...however i am still receiving an undefined variable export...how do i return/pass the export and select statements from the function? i dont know if i am saying it right but in c/c++ at the end of the function i could throw in a return ($export); Same in php. functions. Quote Link to comment https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/#findComment-786208 Share on other sites More sharing options...
pozer69 Posted March 16, 2009 Author Share Posted March 16, 2009 right now i have the following code entered and it works...is there any other way or should i just let it be? global $select, $export; Quote Link to comment https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/#findComment-786209 Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 Yes, there are other ways. Pass the variables to the function as arguments. Quote Link to comment https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/#findComment-786214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.