Jump to content

PHP/MySQL - form and selection


Tanja
 Share

Recommended Posts

I have this problem with mysql/php. I’m Trying to make a gift list where there is possibility to choose one, two or some other amount from certain product. I have one database from where the products transfers to web page and one database for the reservation. Now I’m stuck, because I don’t know how to set the code that people can choose 1 or 2 piece instead of all six pieces.Can anybody help me?Here is the form I have madeecho "<form method=post action=giftlist.php>";echo "<input type=hidden name=save value=1>";echo "<input type=hidden name=modifyid value=$_POST[giftlist]>";echo "<table>";echo "<table border=4 cellpadding=4 cellspacing=4 id=giftlist align=center>";echo "<tr class=heading>";echo "<td>Product</td>";echo "<td>Price</td>";echo "<td>Pieces</td>";while ($rowi=mysql_fetch_array($gift)) {echo "<tr>";echo "<td>".$row[Product]."</td>";echo "<td>".$rivi[Price]."</td>";echo "<td><select><option value=0>".$row[Pieces]."</option></select></td>";echo "<td><input type=submit value=reserve> <onClick=document.location.href='giftlist.php'></td>";echo "</tr>";}echo "</table>";echo "</form>";Thanks in advance

Link to comment
Share on other sites

Currently my server is down. So i cant test it. But i think you need something like this

echo "<form method=\"post\" action=\"giftlist.php\">";echo "<input type=\"hidden\" name=\"save\" value=\"1\">";echo "<input type=\"hidden\" name=\"modifyid\" value=\"$_POST['giftlist']\">";echo "<table border=\"4\" cellpadding=\"4\" cellspacing=\"4\" id=\"giftlist\" align=\"center\">";echo "<tr class=\"heading\">";echo "<td>Product</td>";echo "<td>Price</td>";echo "<td>Pieces</td></tr>";while ($rowi=mysql_fetch_array($gift)) {echo "<tr>";echo "<td>".$row['Product']."</td>";echo "<td>".$rivi['Price']."</td>";echo "<td><select name=\"$row['Product']\"><option value=\"0\">".$row[Pieces]."</option></select></td>";echo "</tr>";}echo "</table>";<input type=\"submit\" value=\"reserve\" />echo "</form>";

The code above is NOT YET COMPLETE*oh my god! you do wanna use <option> (pull down menu with default value, instead of a text box to key in) for clients to choose how much each kind of product they wanna reserve? It is of course possible. Yet at least i needa know what is the maximum quantity. Lets say, from 0, 1, 2, 3, up to HOW MANY??Regarding what you asked, at least you need a name to identify each POST fields :)cheersmidnite

Link to comment
Share on other sites

oh my god! you do wanna use <option> (pull down menu with default value, instead of a text box to key in) for clients to choose how much each kind of product they wanna reserve? It is of course possible. Yet at least i needa know what is the maximum quantity. Lets say, from 0, 1, 2, 3, up to HOW MANY??Regarding what you asked, at least you need a name to identify each POST fields :)cheersmidnite
I don't know if I wanna use <option>, but it seemed the easiest way to get the pull down menu from the quantity from the database. The maximum quantity is 6 but it differ from product to product.
Link to comment
Share on other sites

ok, so i assume you will get the max quantity of each item from the database.

<?phpecho "<form method=\"post\" action=\"giftlist.php\">";echo "<input type=\"hidden\" name=\"save\" value=\"1\">";echo "<input type=\"hidden\" name=\"modifyid\" value=\"$_POST['giftlist']\">";echo "<table border=\"4\" cellpadding=\"4\" cellspacing=\"4\" id=\"giftlist\" align=\"center\">";echo "<tr class=\"heading\">";echo "<td>Product</td>";echo "<td>Price</td>";echo "<td>Pieces</td></tr>";while ($row=mysql_fetch_array($gift)) {echo "<tr>";echo "<td>".$row['Product']."</td>";echo "<td>".$row['Price']."</td>";echo "<td><select name=\"$row['Product']\">";for ($i=0; $i<=$row['max']; $i++)  if ($i == $row['Pieces'])	echo '<option value="', $i, '" selected="selected">', $i, '</option>';  else	echo '<option value="', $i, '">', $i, '</option>';echo '</select></td></tr>';}echo '</table>';echo '<input type="submit" value="reserve" />';echo '</form>';?>

again, no server for testing. But i believe it wont be far from correct :)midnite

Link to comment
Share on other sites

I only got zeros to my database and in the page also :)
i dont get what you mean exactly. Do you already have data in your database? My suggested codes above is just for retrieving data from the database. You probably need another script "giftlist.php" to store/update data to your database =)midnite
Link to comment
Share on other sites

i dont get what you mean exactly. Do you already have data in your database? My suggested codes above is just for retrieving data from the database. You probably need another script "giftlist.php" to store/update data to your database =)midnite
I have two databases. One with Product, amount and price and second with product, price and reserver. Now I get the info to the second database, but all the saved info is zeros :) The code is like this :include "database.php";$gift=mysql_query ("select * from giftlist");if ($_POST[save]) { $reservation = "insert into giftreservation values (null, '$_POST[product]', '$_POST[pieces]', '$_POST[name]')"; mysql_query($reservation); echo "<div class=link><a href=thanks.html>thank you </a></div>"; echo mysql_error(); }echo "<form method=post action=giftlist.php>";echo "<input type=hidden name=save value=1>";echo "<input type=hidden name=modifyid value=$_POST[giftreservation]>";echo "<table>";echo "<table border=4 cellpadding=4 cellspacing=4 id=giftlist align=center>";echo "<tr class=heading>";echo "<td>Product</td>";echo "<td>Price</td>";echo "<td>Amount</td>"; while ($row=mysql_fetch_array($gift)) { echo "<tr>"; echo "<td>".$row[Product]."</td>"; echo "<td>".$ow[Price]."</td>"; echo "<td><select>".$row[Pieces].""; for ($i=0; $i<=$row["max"]; $i++) if ($i == $row[Amount]) echo "<option value= $i> $i</option>"; else echo "<option value= $i> $i</option>"; echo "</td></select>"; echo "<td><textarea name=Nimi>".$varaus[Nimi]."</textarea></td>"; echo "<td><input type='submit' value='Varaa' onClick=document.location.href='lahjavaraus.php'></td>"; echo "</tr>"; } echo "</table>";echo "</form>";
Link to comment
Share on other sites

Hi Tanja,As i see in your query, you have an auto-increment field, right?One suggestion to the database design:(Product, amount, price)(the_auto_increment_field, Product, quantity, reserver)If Product isn't an integer. This will be even better:(Product_id, Product_name, amount, price)(the_auto_increment_field, Product_id, quantity, reserver)Oh i see the whole picture of your idea. Here's may be the code you want :)

include "database.php";$gift=mysql_query ("select * from giftlist");if ($_POST[save]) {  $reservation = "insert into giftreservation values (null, $_POST['Product_id'], $_POST['quantity'], $_POST['reserver'])";  mysql_query($reservation);  echo "<div class=link><a href=thanks.html>thank you </a></div>";  echo mysql_error();}echo "<form method=post action=giftlist.php>";echo "<input type=hidden name=save value=1>";echo "<input type=hidden name=modifyid value=$_POST[giftreservation]>";echo "<table>";echo "<table border=4 cellpadding=4 cellspacing=4 id=giftlist align=center>";echo "<tr class=heading>";echo "<td>Product</td>";echo "<td>Price</td>";echo '<td>Amount</td>';echo '<td colspan="2"'>$nbsp;</td></tr>';while ($row=mysql_fetch_array($gift)) {  echo "<tr>";  echo '<td><input type="hidden" name="Product_id" value="', $row['Product_id'], '" />', $row[Product_name], '</td>';  echo '<td>', $row[Price], '</td>';  echo '<td><select name="quantity">';  for ($i=0; $i<=$row['amount']; $i++)	echo '<option value="', $i, "'>", $i, '</option>';  echo "</td></select>";  echo "<td><textarea name=Nimi>".$varaus[Nimi]."</textarea></td>";  echo "<td><input type='submit' value='Varaa' onClick=document.location.href='lahjavaraus.php'></td>";// i am not sure why you have to onClick=document.location.href='lahjavaraus.php'  echo "</tr>";}echo "</table>";echo "</form>";

Try to use single quote ' instead of double quotes ". It runs faster :) and also we can keep double quotes " in the HTML attributes (which is W3C standard).The above code you provided is not complete, right? (As i see no starting tags likes <html>, <body>, <?php) Remember to close the mysql connection somewhere :)And also, you should have somewhere else to POST reserver, haven't you?One question, just curios. Why people really like performing update operations (like SQL insert/update/delete) on the same page with displaying the HTML contents? i was used to do them in separate files. Which is better?

Link to comment
Share on other sites

One question, just curios. Why people really like performing update operations (like SQL insert/update/delete) on the same page with displaying the HTML contents? i was used to do them in separate files. Which is better?

Well I suppose it reduces the overhead of having to redirect, and also reduces the amount of files you have to use. I suppose its up to the individual, which ever you find less confusing.

Link to comment
Share on other sites

One question, just curios. Why people really like performing update operations (like SQL insert/update/delete) on the same page with displaying the HTML contents? i was used to do them in separate files. Which is better?

Well I suppose it reduces the overhead of having to redirect, and also reduces the amount of files you have to use. I suppose its up to the individual, which ever you find less confusing.

Oh yes! You are right. i just have to spend a boolean isset() checking trades of the header() overhead. It should be effectively faster. i should stick to this approach for my future PHP scripts. Thanks Synook!! And also thanks Tanja for this inspiration :)midnite
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...