tags:

views:

353

answers:

3

Hello,

I am trying to pass multiple variables in a URL in PHP to GET some info, but I don't think it's working.

$allowedFunctions = array(
   'returnAllProducts',
   'refreshCurrentProduct'

);


$IDNUM = $_GET[ 'idNum' ];


$functionName = $_GET[ 'func' ];

if( in_array( $functionName, $allowedFunctions ) && function_exists( $functionName ) )
{
    $functionName();
}

Then I have the refreshCurrentProduct function:

function refreshCurrentProduct() { 
$dbh=mysql_connect ("DATABASE","USER", "PASS") or die('I cannot connect to the database because:'. mysql_error());

mysql_select_db("TABLE");

$query = "SELECT `ID` FROM `PRODUCTS`";

$result = mysql_query($query) or die('Query failed:'.mysql_error());

$DB_STOCK = mysql_query("SELECT `STOCK` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());

$DB_SHORT = mysql_query("SELECT `MYNAME` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());

$DB_LONG = mysql_query("SELECT `DESCRIPTION` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());

$DB_PRICE = mysql_query("SELECT `PRICE` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());

$DB_SHIP = mysql_query("SELECT `SHIPPING` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());


$ID = mysql_result($result,$IDNUM,"ID");
    $STOCK = mysql_result($DB_STOCK,$IDNUM,"STOCK");
    $SHORT = mysql_result($DB_SHORT,$IDNUM,"MYNAME");
    $LONG = mysql_result($DB_LONG,$IDNUM,"DESCRIPTION");
    $PRICE = mysql_result($DB_PRICE,$IDNUM,"PRICE");        
    $SHIP = mysql_result($DB_SHIP,$IDNUM,"SHIPPING");

    echo '
    //echo $STOCK, $SHORT, etc....

    ';
 }

The URL I am using is products.php?func=refreshCurrentProduct&idNum=4

In theory, that should display from the row with 4 in it, however, it only displays the info from the first row. If I do a $IDNUM=5 within the function, it will display the 5th row, so something is wrong with how I pass the information.

Also, how do I create (for instance) $STOCK without having to have so much code in $DB_STOCK? Seems like there has to be a better way...

+2  A: 

Take a look at call_user_func.

$functionName = $_GET[ 'func' ];

if( in_array( $functionName, $allowedFunctions ) && function_exists( $functionName ) )
{
    call_user_func($functionName);
}

Also, if I'm reading your code right, you could get all of the info in a single query:

$query = "SELECT `ID`,`STOCK`,`MYNAME`,`DESCRIPTION`,`PRICE`,`SHIPPING` FROM `PRODUCTS`";
$result = mysql_query($query) or die('Query failed:'.mysql_error());
while ($row = mysql_fetch_assoc($result)) {
    $ID=$row['ID'];
    //etc.
}
AJ
Not quite, the OP wants to get a certain row, not all rows.
Felix Kling
Btw `$functionName()` is not the OPs problem. This works as expected.
Felix Kling
+2  A: 

Why don't you do (as others already mentioned , $IDNUM is not in the scope of the function):

function refreshCurrentProduct() { 
    $dbh=mysql_connect ("DATABASE","USER", "PASS") or die('I cannot connect to the database because:'. mysql_error());
    mysql_select_db("TABLE");

    // If $_GET['idNum'] is not a number use 0
    $rowNumber = is_numeric($_GET['idNum']) ? $_GET['idNum'] : 0;

    $query = "SELECT ID, STOCK,  MYNAME, DESCRIPTION, PRICE, SHIPPING FROM `PRODUCTS`";
    $result = mysql_query($query);

    if(mysql_data_seek($result,  $rowNumber)) {
        // The result set has indeed at least $rowNumber rows

        $row = mysql_fetch_assoc($result);

        echo $row['ID'];
        echo $row['STOCK'];
        // ... etc ....
    }
    else {
        echo "No such row!";
    }
}

No need to hit the database six times! Of course you need to add error handling.

Btw. is the parameter idNum the same as the ID of the record in the database? If so, you can even further simplify:

function refreshCurrentProduct() { 
    $dbh=mysql_connect ("DATABASE","USER", "PASS") or die('I cannot connect to the database because:'. mysql_error());
    mysql_select_db("TABLE");

    // If $_GET['idNum'] is not a number use 0
    $id = is_numeric($_GET['idNum']) ? $_GET['idNum'] : 0;

    $query = "SELECT ID, STOCK,  MYNAME, DESCRIPTION, PRICE, SHIPPING FROM `PRODUCTS` WHERE ID = $id";
    $result = mysql_query($query);

    if (mysql_num_rows($result) == 0) {
       echo "No rows found, nothing to print";
       return;
    }

    $row = mysql_fetch_assoc($result);

    echo $row['ID'];
    echo $row['STOCK'];
    // ... etc ....
}
Felix Kling
+2 for SQL improvements
outis
Thank you Felix!
Jared
+1  A: 

Your $IDNUM variable is outside the scope of your function. You either need to pass that into your function as a variable or you should be able to set it within the function by setting it inside.

function refreshCurrentProduct() { 
  $IDNUM = $_GET[ 'idNum' ];
  ...
}
Shawn Steward
Thank you for your help!
Jared