Getting Values From MySQL to display them on PHP

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
PascalNouma
Forum Newbie
Posts: 1
Joined: Sun May 23, 2010 1:19 am

Getting Values From MySQL to display them on PHP

Post by PascalNouma »

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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Getting Values From MySQL to display them on PHP

Post by social_experiment »

Have you created any code that you can paste?
PascalNouma wrote: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.
The first script that displays the checkboxes for the caketypes might look something like this

Code: Select all

<?php //connect to your database server \ select the database 
//see if there are any rows in the table
//'id' should preferably be a primary key (PK) or something unique to each record
$count = "SELECT COUNT(id) FROM table";
$count_sql = mysql_query($count);
$rows = mysql_fetch_array($count_sql);
$records = $rows[0];
//start populating the page
if ($records != 0) {
 $query = "SELECT caketype FROM cake";
 $sql = mysql_query($query);
 //start creating the form 
 echo '<form method="post" action="page_to_process" >';
 while ($caketype_array = mysql_fetch_array($sql)) {
  echo "".$caketype_array['caketype']." <input type=\"checkbox\" value=\"".$caketype_array['caketype']."\" name=\"cake_type\" />";
  echo '<br />';
 }
 echo '<input type="submit" name="submit" value="Get cake value" />';
 echo '</form>';
} ?>
The process page

Code: Select all

<?php if (isset($_POST['submit'])) {
 $cake_type = $_POST['cake_type'];
 $count = "SELECT COUNT(id) FROM cake WHERE caketype = '".mysql_real_escape_string($cake_type)."'";
 $count_sql = mysql_query($count);
 $rows = mysql_fetch_array($count_sql);
 $records = $rows[0];
 // start populating the page
 if ($records != 0) {
 $query = "SELECT cakevalue FROM cake WHERE caketype = '".mysql_real_escape_string($cake_type)."'";
 $sql = mysql_query($query);
 //echo results 
  while ($cakevalue_array = mysql_fetch_array($sql)) {
  echo 'Cake value is : '.$cakevalue_array['cakevalue'];
  echo '<br />';
  }
 }
} ?> 

Im assuming you will have only one entry for each 'caketype' and 'cakevalue' i.e
cake
----------
caketype | cakevalue
-------------------------
choc_cake | unique_value0
white_cake | unique_value1
------------------------------------
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply