Page 1 of 1

PHP/Mysql <select> problem

Posted: Sat Nov 21, 2009 3:59 am
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

Re: PHP/Mysql <select> problem

Posted: Sat Nov 21, 2009 8:29 am
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();
?>

Re: PHP/Mysql <select> problem

Posted: Sat Nov 21, 2009 11:39 am
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:

Re: PHP/Mysql <select> problem

Posted: Wed Dec 02, 2009 10:37 am
by Nokia N93


Anu HELP Pllllllllllllllllllz