turning database fetched values into $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

revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

I figured out what the warning/ error is from.

Its because i have SESSIONS using newly created variables that are 'child' variables that are dependent on select menu variables.

so say for example you have a select menu that has 2 options.

option 1 = quantity
option 2 = price

and if you make a $new_variable that does something like

(example)

Code: Select all

$new_variable = $quantity * $price;


assuming the quantity and price $variables are already created.


But then what happens is, if you use a regular SESSION like as example:

Code: Select all

<?php 
session_start(); 

    if (isset( $_POST['quantity'], $_POST['price'])) { 
    
           $_SESSION['quantity'] = $_POST['quantity'];
           $_SESSION['price'] = $_POST['price']; 
 ---->   $_SESSION['new_variable'] = $_POST['new_variable']; 
        
    }
?>
Right where the arrow is, is causing the problem. Because that variable cant be 'set', since its a new one.
When i put that in. The Warning/error shows.
It will work and actually pass the data and everything, but it puts that Warning on the page and thats no good.


So , how can I pass this variable as its doing, but do it in a way that warning/error wont pop up across the page?
I'm considering only passing the top level raw data, and doing the math functions on other pages. That would sorta suck.
So is there a simple way to pass that data? or does the math have to be done on each new page with the raw data from select menu values?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

revocause wrote:----> $_SESSION['new_variable'] = $_POST['new_variable'];
If this triggers the warning $_POST['new_variable'] is either not set or NULL.
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

I have the whole thing working just exactly as it should be, and its passing all the of the correct $variables.
Everything is working.. EXCEPT for that warning message on my select menu page as soon as you hit the submit button .

It looks normal, then you hit 'submit' button .
I even have all the $variables echoing out to that page to make sure its doing the math right.
They all echo out and are all exactly as they should be.

but at the bottom of page is this warning:

Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0


But yet everything OTHER than that seems to be working fine.
I can even go to the next page, and see all of the $variables echoed out and everything is right.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

volka wrote:is there a call to session_register? [yes | no | i don't understand]
From the manual...
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Use this instead...

Code: Select all

<?php 
session_start(); 

    if (isset( $_POST['quantity'], $_POST['price'])) { 
    
           $_SESSION['quantity'] = $_POST['quantity'];
           $_SESSION['price'] = $_POST['price']; 
           $_SESSION['new_variable'] = (isset($_POST['new_variable'])) ? $_POST['new_variable'] : ""; 
        
    }
?>
If the new_variable element is supposed to be empty anyway just do this...

Code: Select all

<?php 
session_start(); 

    if (isset( $_POST['quantity'], $_POST['price'])) { 
    
           $_SESSION['quantity'] = $_POST['quantity'];
           $_SESSION['price'] = $_POST['price']; 
           $_SESSION['new_variable'] = ""; 
        
    }
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

AKA Panama Jack wrote:
volka wrote:is there a call to session_register? [yes | no | i don't understand]
From the manual...
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().
Yes, and there was no all to session_register, which is good.
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

hey panama jack,

How would i call that SESSION on the next page ?

I tried what you posted to set the SESSIONS,
and then on the next page i used.

Code: Select all

<?php 
session_start(); 

$_SESSION['new_variable'];

?>
and that isnt working. Do i have to include the whole :

Code: Select all

$_SESSION['new_variable'] = (isset($_POST['new_variable'])) ? $_POST['new_variable'] : "";
I didnt think so, but maybe im wrong : )


Volka, not there isnt any old php version session_register() .
only straight forward new php SESSION .
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

You HAVE to assign the new element something even if it is an empty value. That is why I posted the second example that had this line in it.

Code: Select all

$_SESSION['new_variable'] = "";
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

I did what you posted, and it doesn't work.

example:

Page 1

Code: Select all

<?php 
session_start(); 

    if (isset( $_POST['quantity'], $_POST['price'])) { 
    
           $_SESSION['quantity'] = $_POST['quantity']; 
           $_SESSION['price'] = $_POST['price']; 
           $_SESSION['new_variable'] = (isset($_POST['new_variable'])) ? $_POST['new_variable'] : ""; 
        
    } 
?>




Then on Page 2

Code: Select all

$_SESSION['new_variable'];
and then i tried it as

Code: Select all

$_SESSION['new_variable'] ="" ;
and neither of those worked.

so i went and put this in Page 1

Code: Select all

<?php 
session_start(); 

    if (isset( $_POST['quantity'], $_POST['price'])) { 
    
           $_SESSION['quantity'] = $_POST['quantity']; 
           $_SESSION['price'] = $_POST['price']; 
           $_SESSION['new_variable'] = ""; 
        
    } 
?>


with the Page 2 combinations of

Code: Select all

$_SESSION['new_variable'];
and

Code: Select all

$_SESSION['new_variable'] ="" ;
and neither of those worked.


panama jack
the 'new_variable' DOES have a value that gets created in a math function down in the page.
its not a NULL . So the variable should be passed in whatever new_variable becomes.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I think we can't help you seeing only those code snippets.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Yeah, I was just going to say something like that. We need to see all of the code that is going into this. So far there hasn't been enough to tell what you are trying to do.
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

Thats because its on the first page : )
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Please post your current version (as one block).
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I edited it down for size, and put together a small version. 
 

The same Warning will pop up here too so it's good example. 
You'd need a database named b1_product to make it actually show. 

here's the code

Code: Select all

<?php 
session_start(); 


    if (isset($_POST['product_main'] )) { 
    
          
           $_SESSION['quantity'] = $row['quantity'];
           $_SESSION['quantity1'] = (isset($_POST['quantity1'])) ? $_POST['quantity1'] : ""; 
    } 
?> 
<?php 
//select menu product_main
function makeSelectMenu($menuName1, $options1, $chosen1) { 
$list1 = ''; 
foreach 
($options1  as $option1 => $text1) { 
$selected1 = ($chosen1 == $option1) ? ' selected="selected"' : ''; 
$list1 .= " <option value=\"$option1\"$selected1>$text1</option>\n"; 
} 
return "\n<select name=\"$menuName1\"  STYLE=\"width: 110 px\" id=\"$menuName1\">\n$list1</select>\n"; 
} 

session_start(); 

$options1 = array( 
'' => 'Please Select ', 
'1' => '500', 
'2 ' => '1000', 
'3 ' => '1500', 
'4 ' => '2000',
'5 ' => '2500',
'6 ' => '3000',
'7 ' => '3500',
'8 ' => '4000',
'9 ' => '4500',
'10 ' => '5000',
'11 ' => '6000',
'12 ' => '7000',
'13 ' => '8000',
'14 ' => '9000',
'15 ' => '10000',

); 
$menuName1 = 'product_main'; 
$chosen1 = ''; 
if (isSet($_POST[$menuName1])) $_SESSION[$menuName1] = $_POST[$menuName1]; 
if (isSet($_SESSION[$menuName1])) $chosen1 = $_SESSION[$menuName1]; 
$selectMenu1 = makeSelectMenu($menuName1, $options1, $chosen1); 

?> 



<?php require_once('connect/mysql_connect.php'); ?>




<?php # db query fetch for quantity, baseprice , weight.
 

if(isset($_POST['product_main']))  
{ 
    
$sql = "SELECT quantity, baseprice, weight FROM b1_product WHERE id = " . $_POST['product_main'];

$res = mysql_query($sql); 
$row = mysql_fetch_assoc($res); 
$quantity = $row['quantity']; 
$baseprice = $row['baseprice']; 
$weight = $row['weight']; 

} 

?>




<!--- Set All static page PHP variables -->
<?php 
// set variables. 

$quantity1 = $quantity;

?>


<?php  mysql_close(); // Close the database connection. ?>


<html> 
<head></head>
<body>
<form method="post" action="test123.php"> 
<center>

<BR><BR>
<BR><BR>

<?php echo $selectMenu1; ?> <BR><br>

<input type="submit" value="Calculate" /> 

<BR><BR>
<BR><BR>

<?php 

echo "Quantity =" . "$quantity1"
    
?>

</form>


</body> 
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Ok, I just wanted to make sure
$_SESSION['quantity'] = $row['quantity'];
that really is line 8 of your script? Then Panama Jack was right all the way, there is no variable $row and that in effect sets $_SESSION['quantity'] to NULL.
On the other hand
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$quantity = $row['quantity'];
here a variable $row exists.
$quantity is set to a value != NULL.
Result: When the script ends there's a global variable having a value != NULL and an element in _SESSION with the same name but NULL as "value".
Locked