PHP/Mysql <select> problem

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

Post Reply
Nokia N93
Forum Newbie
Posts: 5
Joined: Sat Nov 21, 2009 3:37 am

PHP/Mysql <select> problem

Post by Nokia N93 »

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

Code: Select all

 
 
<?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
User avatar
BlaineSch
Forum Commoner
Posts: 28
Joined: Sun Jun 07, 2009 4:28 pm
Location: Trapped in my own little world.

Re: PHP/Mysql <select> problem

Post by BlaineSch »

Nokia N93 wrote: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

Code: Select all

 
 
<?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
Your code looks fine, is it giving you an error?

$choice does not seem to be defined anywhere but that should probably be the product ID or something. Once you get that just go back into the database and extract it based on those ID's and the quanity of each product.

Code: Select all

<?php
mysql_connect("localhost");
mysql_select_db("db");
 
if($_POST) {
    $total = 0;
    foreach($_POST as $id=>$value) {
        $query = mysql_query("SELECT * FROM `products` WHERE `id`=$id");
        $row = mysql_fetch_assoc($query);
        $total += ($value*$row['price']);
    }
}
echo $total;
$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=".$row['id'].">";
for($i=0; $i<=$avilable; $i++)
{
echo "<option value=".$i." name=".$i.">$i</option>";
} 
echo "<td >$price</td> 
</tr>";
}
mysql_close();
?>
Nokia N93
Forum Newbie
Posts: 5
Joined: Sat Nov 21, 2009 3:37 am

Re: PHP/Mysql <select> problem

Post by Nokia N93 »

BlaineSch wrote:
Your code looks fine, is it giving you an error?

$choice does not seem to be defined anywhere but that should probably be the product ID or something. Once you get that just go back into the database and extract it based on those ID's and the quanity of each product.

Code: Select all

<?php
mysql_connect("localhost");
mysql_select_db("db");
 
if($_POST) {
    $total = 0;
    foreach($_POST as $id=>$value) {
        $query = mysql_query("SELECT * FROM `products` WHERE `id`=$id");
        $row = mysql_fetch_assoc($query);
        $total += ($value*$row['price']);
    }
}
echo $total;
$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=".$row['id'].">";
for($i=0; $i<=$avilable; $i++)
{
echo "<option value=".$i." name=".$i.">$i</option>";
} 
echo "<td >$price</td> 
</tr>";
}
mysql_close();
?>

thaaaanx for ur help
as i said before im not an expert, i tried the code, nut NOTHING happened
i can't find where is the INSERT statement, nothing inserted in the database for the selection

i'm really frustrated :banghead: :banghead:
Nokia N93
Forum Newbie
Posts: 5
Joined: Sat Nov 21, 2009 3:37 am

Re: PHP/Mysql <select> problem

Post by Nokia N93 »



Anu HELP Pllllllllllllllllllz
Post Reply