Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/182383-problem-with-phpmysql/
Share on other sites

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
}

 

 

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: :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  :(

<?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.

<?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 :shrug:, the problem that i faced is how to remeber the selection "choice" for every row  :confused: :confused:

 

I couldn't figure out how to save the selection for every row and save it to the database

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.