Nokia N93 Posted November 21, 2009 Share Posted November 21, 2009 PHP_Mysql <select> problem? Hi all, i'm new to PHP and i don't know if i can ask questions here or not but i have a little store i want in the databse to store the avilable amount of a product, and list it in a <select> statemnet, when the user choose from the select list , the number is stored in the databse so that i can retrieve it later. i want this process to repeat for all the products i have i started a code , but i couldn't know how to store the selection that the user choose <?php mysql_connect("localhost"); mysql_select_db("db"); $sql = "SELECT * FROM store"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { $p = $row[p]; $av = $row[av]; $price = $row[price]; echo "<tr> <td>$p</td> <td><select name=".$choice.">"; for($i=0; $i<=$avilable; $i++) { echo "<option value=".$i." name=".$i.">$i</option>"; } echo "<td >$price</td> </tr>"; } mysql_close(); ?> after that when i click submit i want a page that prints out for every product the amount that the user choose + the price and the total price i hope you can help me Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/ Share on other sites More sharing options...
rarebit Posted November 21, 2009 Share Posted November 21, 2009 I think you need to close your select tag... Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/#findComment-962447 Share on other sites More sharing options...
Nokia N93 Posted November 22, 2009 Author Share Posted November 22, 2009 rarebit thnx alot for ur help everything in the code is Okay but i need the way to store the selection of the user in the databse and retrieve it back Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/#findComment-963424 Share on other sites More sharing options...
mikesta707 Posted November 22, 2009 Share Posted November 22, 2009 There are quite a few problems with this script. For one I don't see where you ever define $available. Nor do I see where you define $choice. Like Rarebit said, you need to close your select tag after the for statement. if you want to access the post variables, you will probably have to go through your database again to get the array key names. like $select = "your select statement"; $query = mysql_query($select); while($row = mysql_fetch_assoc($query)){ //how ever you define $choice do it here $post = $_POST[$choice];//this is the amount they selected $price = $row['price'];//price $total = $post * $price;//total price } Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/#findComment-963451 Share on other sites More sharing options...
Nokia N93 Posted November 22, 2009 Author Share Posted November 22, 2009 thanx for ur reply but i faced a problem that i have more than one select statmenet so when i click Post, nothing happened :confused: I want when i click submit, the selection to be stored in the database I s this difficult or it is easy ?? I think that i didn't explain the problem right Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/#findComment-963477 Share on other sites More sharing options...
Nokia N93 Posted November 28, 2009 Author Share Posted November 28, 2009 plllllllllllllllz Any help !!!!!!!! :confused: Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/#findComment-966838 Share on other sites More sharing options...
FaT3oYCG Posted November 28, 2009 Share Posted November 28, 2009 you need to put your selects into a form and use the post method. EDIT: Re read ill knock something quick up for you. Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/#findComment-966844 Share on other sites More sharing options...
FaT3oYCG Posted November 28, 2009 Share Posted November 28, 2009 <?php mysql_connect("localhost"); mysql_select_db("db"); $sql = "SELECT * FROM store"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { /* What are these variables? */ $p = $row[p]; // Is this the product name? $av = $row[av]; // Is this the number available? $price = $row[price]; // I'm guessing this is the price ?> <tr> <td><?php echo($p); ?></td> <td> <select name="<?php echo($choice); ?>"> <?php for($i=0; $i<=$avilable; $i++) // $available is not defined should this be a reference to $av ? { // The next line will simply displat a list of items called 1 to 10 for example // the echo inside the value and name should reference $p the product? ?> <option value="<?php echo($i); ?>" name="<?php echo($i); ?>"><?php echo($i); ?></option> <?php } ?> </td> <td ><?php echo($price); ?></td> </tr> <?php } // None of the above code is in a form so how would it get submitted anyway? // neither is it defined in a table an example // <form action="process.php" method="post"> // <table> // <!-- The above data would go here --> // <tr> // <td><input type="submit" name="submit" value="Submit"></td> // </tr> // </table> // </form> mysql_close(); ?> read the comments and respond acordingly. Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/#findComment-966848 Share on other sites More sharing options...
Nokia N93 Posted December 2, 2009 Author Share Posted December 2, 2009 <?php mysql_connect("localhost"); mysql_select_db("db"); $sql = "SELECT * FROM store"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { /* What are these variables? */ $p = $row[p]; // Is this the product name? $av = $row[av]; // Is this the number available? $price = $row[price]; // I'm guessing this is the price ?> <tr> <td><?php echo($p); ?></td> <td> <select name="<?php echo($choice); ?>"> <?php for($i=0; $i<=$avilable; $i++) // $available is not defined should this be a reference to $av ? { // The next line will simply displat a list of items called 1 to 10 for example // the echo inside the value and name should reference $p the product? ?> <option value="<?php echo($i); ?>" name="<?php echo($i); ?>"><?php echo($i); ?></option> <?php } ?> </td> <td ><?php echo($price); ?></td> </tr> <?php } // None of the above code is in a form so how would it get submitted anyway? // neither is it defined in a table an example // <form action="process.php" method="post"> // <table> // <!-- The above data would go here --> // <tr> // <td><input type="submit" name="submit" value="Submit"></td> // </tr> // </table> // </form> mysql_close(); ?> thank you for your help , i have a form but i didn't write it here I did all exactly what you've mentioned , the problem that i faced is how to remeber the selection "choice" for every row :confused: I couldn't figure out how to save the selection for every row and save it to the database Quote Link to comment https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/#findComment-969829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.