Jump to content

Getting Values From MySQL to display them on PHP


PascalNouma

Recommended Posts

Hello everyone,

 

I've 2 tables in mysql file and 2 php files. The first php page will display checkboxes and once I choose checkboxes and then press submit button, it will produce 2nd php page that displays the values of checkboxes.

 

Let's say i've a table called CAKE that has attributes called CAKETYPE & CAKEVALUE. I'll have a php page that shows checkboxes for caketypes and once I choose CAKETYPE(s) on this PHP pages, 2nd php page should display the value of the CAKETYPE(s) (CAKEVALUE) that I chose.

 

Hope you understood what I meant. If you can give me a weblink that explains similar case, it will be great.

 

Thanks a lot from now on.

 

[attachment deleted by admin]

If I understand you correctly, you have the following SQL table

----------dbname.cake---------

---caketype------cakevalue---

---chocolate-------9.99----

---marble-------- 10.99----

----wedding----50.99---

 

So the first page would have:

<form name="pickacake" action="getprice.php" method="post">
<input type="checkbox" name="chocolate" value="1"> Chocolate<br>
<input type="checkbox" name="marble" value="1"> Marble<br>
etc...
</form>

The second page will see which checkboxes are checked, then return the prices for the cake?

<?php
require_once("accessdb.php");

$chocolate = $_POST['chocolate'];
$marble = $_POST['marble'];
$wedding = $_POST['wedding'];
echo "<table border=\"1\"><tr><td>Cake Type</td><td>Cake Value</td></tr>";
if($chocolate == 1){
   $sql = mysql_query("SELECT * FROM cake WHERE caketype='chocolate'");
        while($row = mysql_fetch_assoc($sql)){
              $price = $row[cakevalue];
              $type = $row[caketype];
             
              echo "<tr><td>$type</td><td>$price</td></tr>";
         }
}else{}

if($marble == 1){
      $sql = mysql_query("SELECT * FROM cake WHERE caketype='marble'");
        etc...

 

There is probably a more simple way to handle the PHP, but that's a method I use that I know works.

If you only want to view the price/value of one type of cake, consider using the following HTML, which creates a dropdown box with caketypes. (The same PHP would process a radio button with name="caketype"):

<form name="pickacake" action="getprice.php" method="post">
<select name="caketype">
<option value="chocolate">Chocolate</option>
<option value="marble">Marble</option>
<option value="wedding">Wedding</option>
</select>

 

Then, your PHP would be:

<?php
require_once("accessdb.php");

$caketype = $_POST['caketype'];

$sql = mysql_query("SELECT * FROM cakes WHERE caketype='$caketype'");
     while($row = mysql_fetch_assoc($sql)){
          echo "Cake: ".$row[caketype]."<br>";
          echo "Price: ".$row[cakevalue]."<p>";
}

 

 

If the Milk Tea Shop page is yours, consider formatting your currency to include decimal places. While most people will understand that ($5.5) is $5.50, it looks more professional if you have the trailing zeros ($1.00 instead of $1).

 

Unfortunately I got this error message:

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\action.php on line 23

 

$sql = mysql_query("SELECT * FROM cake WHERE caketype='chocolate'");        while($row = mysql_fetch_assoc($sql)){              $price = $row[cakevalue];              $type = $row[caketype];                          echo "<tr><td>$type</td><td>$price</td></tr>";        }

 

How could I fix this?

 

Thanks for your interest and your help.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.