turning database fetched values into $variables?
Moderator: General Moderators
What's this code block supposed to do?revocause wrote:<?php
session_start();
if (isset($_POST['product_main'] )) {
$_SESSION['quantity'] = $row['quantity'];
$_SESSION['quantity1'] = (isset($_POST['quantity1'])) ? $_POST['quantity1'] : "";
}
?>
What is $row supposed to be here? What is $row['quantity'] supposed to be? What's the source of $row?
Thats a select menu . a specific type of select menu that saves the user selections if they come back to the page or when page reloads.
When they select an option within that 'product_main' select menu, it them uses the value of that option as the database ID number to a product.
it then only fetches data from that row .
example :
id | quantity | baseprice| weight |
------------------------------------------
1 | 100 | 250 | 45
It already works, but its giving that warning.
I then set that in SESSIONS , Im trying different ways to pass the variables in SESSIONS without getting that warning.
When they select an option within that 'product_main' select menu, it them uses the value of that option as the database ID number to a product.
it then only fetches data from that row .
example :
id | quantity | baseprice| weight |
------------------------------------------
1 | 100 | 250 | 45
It already works, but its giving that warning.
I then set that in SESSIONS , Im trying different ways to pass the variables in SESSIONS without getting that warning.
Ok, that's the _POST part. But it still doesn't explain $_SESSION['quantity'] = $row['quantity']; there. $row is only set at one place in your scriptrevocause wrote:a specific type of select menu that saves the user selections if they come back to the page or when page reloads.
and that's much further down in the file.revocause wrote:$row = mysql_fetch_assoc($res);
please try
Code: Select all
session_start();
if (isset($_POST['product_main'] )) {
if ( !isset($row['quantity']) ) {
die(__FILE__.'@'.__LINE__.': there is no $row defined here');
}
else if ( !isset($row['quantity']) ) {
die(__FILE__.'@'.__LINE__.': there is no $row[quantity] defined here');
}
$_SESSION['quantity'] = $row['quantity'];
$_SESSION['quantity1'] = (isset($_POST['quantity1'])) ? $_POST['quantity1'] : "";
}That made it so that it wont work now.
Before, it was working ..except posting that warning.
Now its only saying = " there is no $row defined here"
but yet there should be, becaus eits fetching those values from the database, because it was just echoing them 10 minutes ago.
Thats the problem as i stated on page 1. I have this whole page working, its fetching AND passing all the variables, the math functions are working just as they should be. Everything would be perfect EXCEPT that warning about SESSION global variables blahblah.
My question was, how to use SESSIONS to get rid of the warning without compromising the functionality.
Before, it was working ..except posting that warning.
Now its only saying = " there is no $row defined here"
but yet there should be, becaus eits fetching those values from the database, because it was just echoing them 10 minutes ago.
Thats the problem as i stated on page 1. I have this whole page working, its fetching AND passing all the variables, the math functions are working just as they should be. Everything would be perfect EXCEPT that warning about SESSION global variables blahblah.
My question was, how to use SESSIONS to get rid of the warning without compromising the functionality.
yes, intentionally.revocause wrote:That made it so that it wont work now.
Then there is no $row defined there and then you can't assgin an element of $row to $_SESSION, can you?Before, it was working ..except posting that warning.
Now its only saying = " there is no $row defined here"
Line 8 of the script you've posted:revocause wrote:but yet there should be, becaus eits fetching those values from the database
Line 69 of the script you've posted$_SESSION['quantity'] = $row['quantity'];
at line 8 there is no $row, it does not exists until the script's execution reaches line 69.$row = mysql_fetch_assoc($res);
But even after the select menu is selected, and the submit button used, your way of using SESSION returns the error now , instead of reloading the page.
Now when I open the page, it opens normal, then i make selections and push the submit button.
and it returns only = " there is no $row defined here"
I understand that in old SESSION that $row wasnt defined. , thats why i was asking if i have to make a session start lower in page after that point or what?
The way you showed doesnt work. Because nothing happens now but that error.
So you're basically pointing out to me what i knew was happening 4 days ago : )
My question was , how do i make this work..without the warning.
ALSO ...
I'm willing to take $row['quantity'] right out of the sessions.
I tried making it
just so that i wouldnt have to use $row undefined in SESSION
That worked too .. but gave the warning of older php version
Now when I open the page, it opens normal, then i make selections and push the submit button.
and it returns only = " there is no $row defined here"
I understand that in old SESSION that $row wasnt defined. , thats why i was asking if i have to make a session start lower in page after that point or what?
The way you showed doesnt work. Because nothing happens now but that error.
So you're basically pointing out to me what i knew was happening 4 days ago : )
My question was , how do i make this work..without the warning.
ALSO ...
I'm willing to take $row['quantity'] right out of the sessions.
I tried making it
Code: Select all
$_SESSION['quantity1'] = $_POST['quantity1'];
//while making the variable conversion like this.
$quantity1 = $quantity;That worked too .. but gave the warning of older php version
Last edited by revocause on Mon Jun 25, 2007 9:20 am, edited 1 time in total.
Yes. When the user presses the submit button a new http request is made to thwe webserver. The webserver starts a new instance of php. session_start() then loades the stored session data to the array _SESSION. But not $row.revocause wrote:But even after the select menu is selected, and the submit button used, your way of using SESSION returns the error now , instead of reloading the page.
If $row was stored, why would you need a session mechanism after all? All variables exception _SESSION are not stored between requests.
Yes, it's only purpose was to show you what's going on.The way you showed doesnt work. Because nothing happens now but that error.
So if i take $row out of the SESSION completely.
and if i make it like this :
and if i use sessions on a new page to try to pick that up . by using
that wont show the passed variable 'quantity'
How can I do that?
and if you put something like
and that takes it back to the warning, because obviously it wont be set yet.
How can i pass the 'quantity' variable. checking that its set, and then pass it without the warning.
and then pick it up on page 2.
Everything that i am trying in all the possible sessions isnt working. (it is, but it includes that warning)
and if i make it like this :
Code: Select all
<?php
session_start();
if (isset($_POST['product_main'] )) {
$_SESSION['quantity1'] = (isset($_POST['quantity1'])) ? $_POST['quantity1'] : "";
}
?>and if i use sessions on a new page to try to pick that up . by using
Code: Select all
<?php
session_start();
$_SESSION['quantity1'];
?>How can I do that?
and if you put something like
Code: Select all
<?php
session_start();
if (isset($_POST['product_main'] )) {
$_SESSION['quantity1'] = $_POST['quantity1'];
}
?>and that takes it back to the warning, because obviously it wont be set yet.
How can i pass the 'quantity' variable. checking that its set, and then pass it without the warning.
and then pick it up on page 2.
Everything that i am trying in all the possible sessions isnt working. (it is, but it includes that warning)
_POST contains the values that the browser sent with this request using the post method.
If you have a html form using the post method all named (and successful) form control items are sent as name=value pairs when the form is submitted (as new http request). Those name=value pairs "appear" in php's $_POST as $_POST['name']='value'.
Your form does not have an item named quantity1 and therefore $_POST['quantity1'] never exists.
take a look at the output of
If you have a html form using the post method all named (and successful) form control items are sent as name=value pairs when the form is submitted (as new http request). Those name=value pairs "appear" in php's $_POST as $_POST['name']='value'.
Your form does not have an item named quantity1 and therefore $_POST['quantity1'] never exists.
take a look at the output of
Code: Select all
session_start();
echo '<pre>_POST: '; print_r($_POST); echo "</pre>\n";
echo '<pre>_SESSION: '; print_r($_SESSION); echo "</pre>\n";
if (isset($_POST['product_main'] )) {I know there isnt any select menu names 'quantity1'
but there is a variable.
the variable $quantity1 exists because im using it on my page 1 and its echoing out.
quantity1 is obviously a newly created variable , but its value is dynamic because its set by the user selections.
So once its created by what the user selects , how do i pass that in SESSIONS?
i even left the value open like this :
and then on Page 2. I tried to pick it up with
Do you want to know how it WILL work?
if i do this..
on Page 1.
on Page 2. i pick up the variable.
When i do it that way the varible is passed toPage 2 and works on Page 1 in my math functions. etc.
It all works great ..EXCEPT that Warning message which says.
"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 there is a variable.
Code: Select all
$quantiy1 = $quantity;the variable $quantity1 exists because im using it on my page 1 and its echoing out.
quantity1 is obviously a newly created variable , but its value is dynamic because its set by the user selections.
So once its created by what the user selects , how do i pass that in SESSIONS?
i even left the value open like this :
Code: Select all
<?php
session_start();
if (isset($_POST['product_main'] )) {
$_SESSION['quantity1'] = $_POST['quantity1'] = '';
}
?>and then on Page 2. I tried to pick it up with
Code: Select all
<?php
session_start();
$_SESSION['quantity1'];
?>Do you want to know how it WILL work?
if i do this..
on Page 1.
Code: Select all
<?php
session_start();
if (isset($_POST['product_main'] )) {
$_SESSION['quantity1'] = $_POST['quantity1'];
}
?>on Page 2. i pick up the variable.
Code: Select all
<?php
session_start();
$_SESSION['quantity1'];
?>When i do it that way the varible is passed toPage 2 and works on Page 1 in my math functions. etc.
It all works great ..EXCEPT that Warning message which says.
"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"
I only read til
The parameters the browser sent are parsed into $_GET/$_POST. Your form uses the post method -> parameters are pares into $_POST.
You call session_start() -> the session data is loaded and stored into _SESSION.
But the new instance has no idea, no idea at all of a variable $quantity or $quantity or $maryhadalittlelamb another instance of php once had. Only _SESSION is stored between requests.
Again: user presses submit button, browser sends new http request, webserver starts new instance of php, new instance, completely new, is not connected to the old instance in any way (more or less).revocause wrote:the variable $quantity1 exists because im using it on my page 1 and its echoing out.
The parameters the browser sent are parsed into $_GET/$_POST. Your form uses the post method -> parameters are pares into $_POST.
You call session_start() -> the session data is loaded and stored into _SESSION.
But the new instance has no idea, no idea at all of a variable $quantity or $quantity or $maryhadalittlelamb another instance of php once had. Only _SESSION is stored between requests.
Great, thats the purpose of my question.
and how do i pass that? as I assumed that since it was placed into SESSION it would store it on that reload if its using an if(isset).
If i knew what that line of code was to pass that variable, I'm assuming i wouldnt be here asking.
Is there a way to make that work without the warning?
and how do i pass that? as I assumed that since it was placed into SESSION it would store it on that reload if its using an if(isset).
If i knew what that line of code was to pass that variable, I'm assuming i wouldnt be here asking.
Is there a way to make that work without the warning?
Trial and error. A tried principle of science which also applies to programming. See if it works, investigate why it hasn't, use the internet as your reference book. Have you actually considered reading the PHP manual regarding sessions or perhaps googling for tutorials?revocause wrote:Is there a way to make that work without the warning?
Yeah , Patrickg .
That wouldve been my first steps .
Seeing as how this forum for beginners isn't the best source for answers sometimes.
It seemsto be more of a forum where they tell you = "we know that answer , but you dont.. why dont you go look for it yourself. "
I'm not a total beginner though. Its just that PHP being open ended language allows you to make new things you may not have made before.
I wasnt trying to ask for tutorials on 'How to program in php 101'
just a question on how to deal with a 'history save' select menu.
I'm aware that when the page reloads, the history of the user selected menu is still kept, the problem is that the values arent.
thats where Im getting my error ,because all those values become NULL .
I have two options,
1. make the user select menu NOT a history of selection functionality.
2. Find out of any one has ever done this before and how they were able to get around that.
Considering i do intend to use the history select version. I'm trying to find a way to be able to reload the page, without destroying all previous variables.
The reason its happening is again, because the if(isset) is showing that the select menu IS set. but it hasnt made a fetch to call those values after its reloaded. So i need to find a way around that problem.
I dont believe its a basic php question. unless you've used a select menu history save before.
If anyone has, I'd be interested to know how YOU did it .
That wouldve been my first steps .
Seeing as how this forum for beginners isn't the best source for answers sometimes.
It seemsto be more of a forum where they tell you = "we know that answer , but you dont.. why dont you go look for it yourself. "
I'm not a total beginner though. Its just that PHP being open ended language allows you to make new things you may not have made before.
I wasnt trying to ask for tutorials on 'How to program in php 101'
just a question on how to deal with a 'history save' select menu.
I'm aware that when the page reloads, the history of the user selected menu is still kept, the problem is that the values arent.
thats where Im getting my error ,because all those values become NULL .
I have two options,
1. make the user select menu NOT a history of selection functionality.
2. Find out of any one has ever done this before and how they were able to get around that.
Considering i do intend to use the history select version. I'm trying to find a way to be able to reload the page, without destroying all previous variables.
The reason its happening is again, because the if(isset) is showing that the select menu IS set. but it hasnt made a fetch to call those values after its reloaded. So i need to find a way around that problem.
I dont believe its a basic php question. unless you've used a select menu history save before.
If anyone has, I'd be interested to know how YOU did it .