tags:

views:

68

answers:

4

Silly question, bare with me (I'm new).

In my includes folder I have a function...

function storelistingUno() {

$itemnum=mysql_real_escape_string($_POST['itemnum']);
$msrp=mysql_real_escape_string($_POST['msrp']);
$edprice=mysql_real_escape_string($_POST['edprice']); //This value has to be the same as in the HTML form file
$itemtype=mysql_real_escape_string($_POST['itemtype']);
$box=mysql_real_escape_string($_POST['box']);
$box2=mysql_real_escape_string($_POST['box2']);
$box25=mysql_real_escape_string($_POST['box25']);
$box3=mysql_real_escape_string($_POST['box3']);
$box4=mysql_real_escape_string($_POST['box4']);
$box5=mysql_real_escape_string($_POST['box5']);
$box6=mysql_real_escape_string($_POST['box6']);
$box7=mysql_real_escape_string($_POST['box7']);
$box8=mysql_real_escape_string($_POST['box8']);
$itemcolor=mysql_real_escape_string($_POST['itemcolor']);
$link=mysql_real_escape_string($_POST['link']);
$test = "yes!";

}

I reference this in about 8 pages and I decided it would be easier to just make a function out of it and only touch this from now on. So I referenced storelistingUno(); in my code, but I don't think it worked, because I tried to execute echo $test; and nothing happend. Do I need to return something ?

Thanks.

+1  A: 

$test is a local variable in that function - you either need to make it global (by putting global $test; at the start of the function or using $GLOBALS['test'] instead of just $test or return the value.

Are you thinking of using that function to just escape the values? Maybe you could make it perform the query too, then you wouldn't have to return / use globals.

Edit: A different way would be to include the code instead of using a function - not recommended though...

Greg
A: 

You would have to mark every variable as global before you start to edit them within the function ... this isn't recommented since it's bad coding style but it might help you

$test = '';
function foo() {
    global $test;
    $test = 'bar';
}
echo $test; //prints nothing
foo();
echo $test; // prints "bar"
pagid
+1  A: 

Look into extract(). You can do something like this:

<?php

function getEscapedArray()
{
  $keys = array('itemnum', 'msrp', 'edprice', 'itemtype', 'box', 'box2', 'box25', 'box3', 'box4', 'box5', 'box6', 'box7', 'box8', 'itemcolor', 'link');

  $returnValues = array();
  foreach ($keys as $key) {
    $returnValues[$key] = mysql_real_escape_string($_POST[$key]);
  }

  $returnValues['test'] = 'yes!';

  return $returnValues;

}

extract(getEscapedArray());
echo $test;

Although - Its still not the best way to do this. The best would be to just use the return from that function as the array.

$parsedVals = getEscapedArray();
echo $parsedVals["test"];
gnarf
+1  A: 

If you absolutely need these variables a globals

function storelistingUno()
{
    $desiredGlobals = array( 
         'itemnum'
        ,'msrp'
        ,'edprice'
        ,'itemtype'
        ,'box'
        ,'box2'
        ,'box25'
        ,'box3'
        ,'box4'
        ,'box5'
        ,'box6'
        ,'box7'
        ,'box8'
        ,'itemcolor'
        ,'link'
    );
    foreach ( $desiredGlobals as $globalName )
    {
        if ( isset( $_POST[$globalName] ) )
        {
            $GLOBALS[$globalName] = mysql_real_escape_string( $_POST[$globalName] );
        }
    }
}
Peter Bailey