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

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

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

revocause wrote:See, i only need the name, weight, baseprice, quantity .. for only the option the user selects.
Where do you let the user select an option? That's the value you'll want to look for in the database.
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

this post no longer valid, as it was to somebody prior to superdezign.

see next post
Last edited by revocause on Tue Jun 19, 2007 9:57 am, edited 2 times in total.
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

in response to superdezign.


Its a select menu 'drop-down'
what im making is a product calculator.

Select Menu
1. quantity
2. size
3. paper
4. folding
5. aqueous
6. color
7. proofs

all of the attributes will be static values. Except quantity, and paper.

When user selects quantity it must spit out = weight, baseprice, quantity.
example: 50 - 650 - 500

but then , I need to turn those values into variables i can use.
So if in my math functions or sessions passing variables if i want to pass the $weight, $baseprice, $quantity.. then it will only pass what the
user had selected.
It all calculates out , and reloads the page to show the quote on the product calculator.
and should save history so that when it reloads, the user can see what they previously selected.
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

Usually i handle less intense php scripts.
So this product calculator is sorta a pain in the butt compared to my php beginner skill levels.

I dont know if its allowed in the rules or not,
but if one of the experts would like to build this , with an explanation so that it could be modified.
I'd pay ya.

Im losing a lot of time just trying to figure it out .
If anyone is able to do this without bugs and errors in it as im getting on my own,
then feel free to private message me a price.

This type of programming is excessive for my role. but we need the product calculator in the 'now'
scenario.

Im not by default the PHP program extraordinaire. Its not in my required skill at the time.
but this product calculator need came along. and so here i am .

If anyone would like to do this priovate message me. Otherwise, I'm trying to figure out how to do it.
thats my motive of being here : )
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post by ReverendDexter »

I'm confused on one point. Are you trying to have your select box filled so that each option in the select appears like:

[50-650-500]

or do you just need those numbers on the next "page" as it were?

If you only want one query going to the database, and you only want one item's info, then just put the primary keys of the option in the html, i.e.

Code: Select all

<select name="your_name">
    <option value="key1">1</option>
    <option value="key2">2</option>
    <option value="key3">3</option>
</select>
Then, when you do your query, you can just use:

Code: Select all

$sql = "SELECT weight, price, quantity, FROM table, WHERE key = " . $_POST['your_name'];

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

If you want price, weight, and quantity to be in the select box's option list AND you need them for your calculations, you're going to have to query the database twice, once to get all the info (as previously posted), and once like I've shown.

Hope that helps!
-Dex
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'll do it, but you'd have to let me explain it to you as we went along. I agree that being here, you should be able to learn it rather than pay for it. I'll take another stab at this though.

You have a form. The form posts the variables. Then, you want to get the item that those variables point to. Right?

The query you're after takes those variables in the WHERE clause, not in the SELECT clause.

Code: Select all

mysql_query("SELECT * FROM whereever WHERE size = '" . $_POST['size'] . "' AND color = '" . $_POST['color'] . "';");
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

ReverendDexter,
did anybody ever tell you that you're a gosh darn genius! : )

Thats what Im talkin about.

Thats exactly what i was tyring to get at .

I was tyring to get the option key ( i was calling it value) to then trigger the function to call
baseprice, weight, and quantity.

and to then turn it into $variables that i could then use to calculate with in math functions.

and you showed it was the = $weight = $row['weight'];

SOB! if that wasnt some light at the end of the darn tunnel I dont know what was.

Thank you ReverencDexter.. today you have heal the php sick..
If i wa sa dog, I'd be making a turtle right now.

I will try that code in a little bit and let ya know, right now Im workin on history of select menu stuff.

SOB the boy's a genius! .. (throws confetti)
.. woohoo
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

theres an error Im getting.


Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in .....(local server blah blah etc)

Code: Select all

<?php 

        $sql = "SELECT quantity, baseprice, weight FROM itemA_product, WHERE id = " . $_POST['selectmenuA'];

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

         ?>



any idea whats goin on there?
I checked the names, and spellings of the database fields, tables etc. All that is good

the values in my select menu are "1" through "15"

so i modified your line that now says: WHERE id= etc.

see anything ?
is it a syntax error or somethin making an error to the database?
the db is connected mysql_connect btw.
all passwords and user name are good, because I've used the connect for others
theres no stream or database connect errors anyway
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You're query is invalid. You have a comma before WHERE.

When you run queries, you can debug with mysql_error().

Code: Select all

mysql_query($query) or die(mysql_error());
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

DOh!~

thanks
them darn simple comma fruit flies got by .
I'll try that
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

oddity alert .

It's still making that error when i first open the page. BUT , then after I select the choices and hit submit button
the warning error disappears.

I assumed this was because all the options have a defualt "Please Select" with no value.
so i gave the that select menu a vlaue of one just so that it would default with that.

but the error still appears , until you select and submit the form.

It's working for fetching those variables though, because i echoed out the
$weight ,$baseprice, $quantity at the bottom of page to check . and its all echoing out good.

I dont know why that error when you first open page though?
possibly because it hasnt 'submit' anything to it yet?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Probably because you use the $_POST array without being sure that it even exists, and without cleaning the data.
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post by ReverendDexter »

Yeah, I should have been clearer about that... my bad.
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

I made a post but it went to 404 page.

so excuse if this one becomes double.


Anyway, i thought about if (isset) before that $_POST

but i figured it would be default set always.

this is back into swamp marsh for my newbie side of this stuff.

can you show me what that would even look like?
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Post by revocause »

dont sweat it Rev
I did modify your code to work with mine.

I just wouldnt have known how to see if it was set or not yet. so
Post Reply