Page 1 of 3

is this code right? - Mysql query from select menu.

Posted: Mon Jun 18, 2007 9:37 am
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]


Hi, 


(this is a new question /topic btw - moderators) 

i have a select menu and when user selects an option, that one option will fetch 3 attributes from database. 
The problem is it isnt working, can you check this code and tell me what is going on? 

in php section, i add sessions:

Code: Select all

<?php 
session_start(); 

if (isset($_POST['AAatt'])) { 

    $_SESSION['quantity'] = $_REQUEST['quantity']; 
    $_SESSION['baseprice'] = $_REQUEST['baseprice']; 
    $_SESSION['weight'] = $_REQUEST['weight'];
}
?>



Then i set the variables

Code: Select all

<?php
$quantity = $_REQUEST['quantity'];
$weight = $_REQUEST['weight'];
$baseprice = $_REQUEST['baseprice'];   
?>



Then in the HTML i make the select menu:

Code: Select all

<select name="AAatt" selected="selected" width="20" >
  <option>Please Select </option>
  <option value="<?php $data = mysql_query("SELECT weight, baseprice, quantity FROM b1_product WHERE id='1' ") or die(mysql_error()); ?> ">500</option>
 
  (continued options - stripped down version for space on fourm )


The connections to database is working, but it isn't returning the variables .
I then try to echo those variables out , but they dont exist.

Code: Select all

<?php
echo  "$weight" .  "$baseprice" . "$quantity"; 
?>

I've tried to $_POST , etc on the variables. doesnt work.

My database is set up like this:

id | name | quantity | baseprice | weight |
-----------------------------------------------------
1 | widgetA | 500 | 650 | 75 |

etc.

Thank ya


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]

Posted: Mon Jun 18, 2007 9:41 am
by Gente

Code: Select all

<option value="<?php $data = mysql_query("SELECT weight, baseprice, quantity FROM b1_product WHERE id='1' ") or die(mysql_error()); ?> ">500</option>
Hmm... What are you expecting from this code? :?

Posted: Mon Jun 18, 2007 10:01 am
by revocause
Somebody told me friday to try that from this forum.

They said that was how i could fetch 3 variables/values from one select menu option.

what I'm tyring to do with it, is connect to database, grab 'weight, baseprice, quantity' from one product
and trying to use that WHERE id= '1' .
to try to pull the info from that horizontal row only.

I know it doesnt work, because it isnt working. lol

Posted: Mon Jun 18, 2007 10:10 am
by Gente

Posted: Mon Jun 18, 2007 1:21 pm
by revocause
I looked at that page, tried the various ways they had queries etc. there was only a couple that wouldve been able to be modified to what i needed, but they dont work .. and further down the page you see all the 're-edits' of the mistakes.

technically what I'd like to know is the correct syntax for making a database query to get 3 different attributes from one select menu option.

I would like the database to only be searched for tose attributes only if that option is selected.

Then i need to turn them into $variables to fo math functions with.

does anyone understand what Im explaining?

so for example:

<select >
<option value = "(database query to get 1. weight, 2. base price, 3. quantity) "

then i need to pick those variables up to make use of them in the php math functions.
I know how to call one variable from database. but when doing 3 in one option select menu, its bunching them all together.
is there a simple syntax to call all three = ''weight", "baseprice", "quantity"

then have them put into an array . example $name = $_POST['name']
?

I've been chasing this one around for a couple weeks.
Thanks for the syntax if anyone knows it .

Posted: Mon Jun 18, 2007 1:30 pm
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]


or for a simple explanation of what is going wrong, 
i use this code in my <select > menu option.

Code: Select all

<option value="
<?php 
$result = mysql_query("SELECT name, quantity, baseprice, weight FROM first_product WHERE id = '1'") or die("Could not perform select query - " . mysql_error());
?> ">500</option>


I echo the $result
just to see what its spitting out

Code: Select all

<?php 
echo $result;
?>


and i get this = Resource id #4


I'm also echoing variables that i converted , for example:

Code: Select all

$product_name = $_POST['name'];
$quantity = $_POST['quantity']; 
$baseprice = $_POST['baseprice'];


and that returns nothing.
right i know, Im a newb when it comes to select menu complexity stuff. It wasnt always a thing that came up
so try not to kick me in the balls too hard ..thanks. lol


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]

Posted: Mon Jun 18, 2007 1:36 pm
by revocause
the php experts are smelling blood in the water ..they're circling, look newbie meat ! . . l o l

ok enough of that, you guys get yours by making a lot more money than us newbs so .. its all fair right?

please answer my question about the syntax above
thanks :)

Posted: Mon Jun 18, 2007 3:36 pm
by superdezign
Seriously man, no one at DevNetwork is prejudiced against new programmers.

Secondly, I don't think you're understanding what Gente was trying to show you. mysql_query queries the database and returns a resource. The resource (Resource id #4) is not a number or a string or a float. It's a resource. It is not be used as data, but to retrieve data. If you read the page that Gente sent you to and looked at the Return Values section, you may have realized this.

This...

Code: Select all

<option value="
<?php
$result = mysql_query("SELECT name, quantity, baseprice, weight FROM first_product WHERE id = '1'") or die("Could not perform select query - " . mysql_error());
?> ">500</option>
... does next to nothing. You query the database, create a resource, and then waste it.

Code: Select all

<?php
$result = mysql_query("SELECT name, quantity, baseprice, weight FROM first_product WHERE id = '1'");
$data = mysql_fetch_object($result);
?>

<option value="<?php echo $data->quantity;?>" />
When members suggest a portion of the PHP manual to you, you should read it rather than skim it. The PHP manual is the reason why PHP is so easy to catch on to because it answers the majority of the questions that you may have.

mysql_query() takes a query as a parameter and returns a MySQL resource.
mysql_fetch_*() takes a MySQL resource as a parameter, returns data, and points the resource to the next result.

Posted: Mon Jun 18, 2007 3:44 pm
by ReverendDexter
revocause wrote: so for example:

<select >
<option value = "(database query to get 1. weight, 2. base price, 3. quantity) "

is there a simple syntax to call all three = ''weight", "baseprice", "quantity"

then have them put into an array . example $name = $_POST['name']
?

I've been chasing this one around for a couple weeks.
Thanks for the syntax if anyone knows it .
Okay, I'm way newb here as well, so let me see if I'm getting what you're trying to do:

You want an HTML select box, that, will, as options, have the weight, base price, and quantity of an object in your db, so that the options might look like this:

[12, $2.95, 15]
[.75, $49.95, 2]
... etc.

When you select that item, you'd like those variables to be placed in an array variable so that they can be accessed for doing some computation.

What's the primary key for the items you're searching for? Basically, what I'm getting at, is why aren't you doing more of a "SELECT weight, price, quantity FROM table WHERE primary_key = $whatever" to get your data, and then a $option_details = mysql_fetch_assoc($row) to get the individual items into an array?

Again, I'm pretty new at this, but hopefully something in there gets you closer!
-Dex

Posted: Mon Jun 18, 2007 4:17 pm
by superdezign
ReverendDexter wrote:why aren't you doing more of a "SELECT weight, price, quantity FROM table WHERE primary_key = $whatever" to get your data
Not to burst your bubble, but that's exactly what he's doing. :P

However, you're completely right as to the problem being the lack of a mysql_fetch_* function being called.


And revocause, if you do want the whole row from the table to be handled as one option, implode() a mysql_fetch_array() array.

Posted: Tue Jun 19, 2007 8:09 am
by revocause
Now everyone is lively : )

that code is great! that you stated above.

There's a problem with it though .

If i do that code for all 8 or 10 select menu options, then it will query ALL of them.

i only want the option selected by a user , to be queried. and stored as that pages response.
for weight, baseprice, etc.

if you do it the way you mentioned, the variables will always be the last select menu query.

so its great, its just that it doesnt work completely.

that was the content of my question : )

Posted: Tue Jun 19, 2007 8:26 am
by revocause
So to refresh a little on the question,
if i have 7 or 8 select menu options . But i only want the:
name, weight, baseprice, quantity ...but, ONLY for the option selected.

Then there has to be more code with the right syntax to seperate the query selected from the others.

I was wondering if there is a way to do one of two things,
1. should i make a code that will only query AFTER the selected choices are made?

or

2. should i use the standard query and fetch result method. and then use a code to select out , highlight, etc the select menu option
that was user selected?

with the basic query , it works to query them all. So say one select menu has say 15 options.
and each of those options is doing a query to database field with cooresponding id number
example: WHERE id ='1'
and there would be 1 through 15 obviously.


I only want the variables for the option that was selected. Thats why in my question, i left the fetch and array part off. to ask if anyone had suggestions on what would be the best method to achieve what I'm trying to do.

Thanks : )

Posted: Tue Jun 19, 2007 8:37 am
by superdezign
Then modify the code to work. Instead of using WHERE id=1 use:

Code: Select all

SELECT name, quantity, baseprice, weight FROM first_product WHERE id < 15;
Or

Code: Select all

SELECT name, quantity, baseprice, weight FROM first_product ORDER BY id ASC LIMIT 0,15;

You want to select them all in on query, then fetch each result one at a time.

Posted: Tue Jun 19, 2007 8:42 am
by revocause
nope thats not the problem either
I have changed all the WHERE id = '1' (2,3, 4,5,6,7,8 etc)

the problem I've bene having, is that the 4 variables (name, weight, baseprice, quantity)
for ALL 15 options is queried.

See, i only need the name, weight, baseprice, quantity .. for only the option the user selects .

because 1 through 15 are different products. and i want only those variables from the selected product.

by doing it the way youre mentioning, it will query those 4 variables. for all 15 products.

if i then make a call to $data->weight

then what i will get isnt the weight form the option the user selected. Instead it will be the 15th option
because each time the database will overwrite the previous
so that the name, weight, baseprice, quantity.. end up being the 15th option. regardless what was user-selected.

Posted: Tue Jun 19, 2007 9:14 am
by superdezign
I don't think you're understanding. I think I'm going to write a tutorial and direct you to it (unless this post clears it up).

The way mysql_query works is it queries the database and gives you ALL of the rows that fit the query.
The way mysql_fetch_* functions work is that they return one row, and then point to the next row (if there is another one left).

Code: Select all

$result = mysql_query("SELECT name, quantity, baseprice, weight FROM first_product ORDER BY id ASC LIMIT 0,15;");

while($data = mysql_fetch_array($result))
{
    echo implode("<br />\n", $data) . "<br />\n";
}
Run that, and you should see what I mean.