Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/149650-solved-php-code-if-elseif-help/
Share on other sites

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']);

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.

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.

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);

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.