Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

price catalogue... open a table at the right place!

Posted on 2005-04-06
21
Medium Priority
?
249 Views
Last Modified: 2006-11-18
Hi! I would like to create a php page that does the following...

i have a table like this

Room Type   PERIOD A  -  PERIOD B -  PERIOD C
-----------------------------------------------------------
A231              300€           400€          500€

B221              330€           420€          200€

C251              300€           400€          500€

C351              300€           400€          500€


I would like a simple selection form where i can choose the room type and the page shows only the right row...

example i open the page and select A231 from a dropdown list... at that moment the page should show this row

A231              300€           400€          500€


the best would be to put the table on a mysql database... just an idea...

thanks!

0
Comment
Question by:catalini
  • 10
  • 7
  • 4
21 Comments
 
LVL 6

Expert Comment

by:peyox
ID: 13718807
This sample script will produce a query, you can see how it works

<?
   if ($_GET['type'])
   {
     // search request submited
     $query = "select * from table where roomtype= '".$_GET['type']."';";

     echo "<b>Sample query:</b><br>";
     echo $query;

     echo "<br><br><a href=".$_SERVER['PHP_SELF'].">Search again</a>";
     die();
   }

   echo "Room type:&nbsp;";
   echo "<form action=".$_SERVER['PHP_SELF']." method=GET>";
   echo "<select name=type>";
   echo "<option id=A231>A231</option>";
   echo "<option id=B221>B221</option>";
   echo "<option id=C251>C251</option>";
   echo "</select>";
   echo "&nbsp;";
   echo "<input type=submit value=Search>";
?>
0
 

Author Comment

by:catalini
ID: 13724556
thanks! what should be the sql code to create the tables in the database? thanks again
0
 

Author Comment

by:catalini
ID: 13724574
and how can i indicate which database should it use? (db password and so on)...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13725113
0
 

Author Comment

by:catalini
ID: 13725153
phpmyadmin already installed... i just would like to have some cut and past code to use for this purpose... this is why this answer is worth 500 points!

thanks
0
 
LVL 25

Assisted Solution

by:Marcus Bointon
Marcus Bointon earned 1000 total points
ID: 13725597
phpmyadmin can generate the table creation SQL for you - just look in the export tab under structure. It's hard for us to give you an answer as you've not really told us much about what you're doing, what your table might have in it, or how it relates to other structures in your DB. At a guess:

CREATE TABLE `rooms` (
  `roomtype` varchar(10) NOT NULL default '',
  `perioda` int(10) unsigned NOT NULL default '0',
  `periodb` int(10) unsigned NOT NULL default '0',
  `periodc` int(10) unsigned NOT NULL default '0',
  KEY `roomtype` (`roomtype`)
) TYPE=MyISAM;

You might want to use DECIMAL type fields instead of integers.

To connect to your database:

$db = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die ('could not connect to DB');
mysql_select_db($db, 'mydatabase');

The easiest way of creating the user ID to access your DB is to use the privileges page in phpmyadmin. Click the home button and you'll find a link to it on the front page.
0
 
LVL 6

Accepted Solution

by:
peyox earned 1000 total points
ID: 13727468
Squinky proposed a table for you, then you have to send the query to db, modified code will look like this:

<?
   if ($_GET['type'])
   {
     // search request submited
     $db = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die ('could not connect to DB');
     mysql_select_db($db, 'mydatabase');

     $query = "select * from rooms where roomtype= '".$_GET['type']."';";

     $result = mysql_query($query);

     while ($row = mysql_fetch_assoc($result))
     {
        echo $row['roomtype']." ".$row['perioda']." ".$row['periodb']." ".$row['periodc']."<br>";
     }
     mysql_free_result($result);

     echo "<br><br><a href=".$_SERVER['PHP_SELF'].">Search again</a>";
     die();
   }

   echo "Room type:&nbsp;";
   echo "<form action=".$_SERVER['PHP_SELF']." method=GET>";
   echo "<select name=type>";
   echo "<option id=A231>A231</option>";
   echo "<option id=B221>B221</option>";
   echo "<option id=C251>C251</option>";
   echo "</select>";
   echo "&nbsp;";
   echo "<input type=submit value=Search>";
?>
0
 

Author Comment

by:catalini
ID: 13728556
i used the code from peyox and the sql of  Squinky...

i changed my db data (name, password and so on) and i receive this error when i try to get a row

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/httpd/vhosts/taunus.it/httpdocs/listino2.php on line 6

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus.it/httpdocs/listino2.php on line 12

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus.it/httpdocs/listino2.php on line 16
0
 
LVL 6

Expert Comment

by:peyox
ID: 13728798
Have you changed this line?
mysql_select_db($db, 'mydatabase');

'mydatabase' - your database name here
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13728916
given that mysql_select_db is failing due to an invalid resource, it must mean that the call to mysql_connect failed - it's not even getting the chance to select the db. Try changing this to get more info:

$db = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die ('could not connect to DB'. mysql_error());
0
 

Author Comment

by:catalini
ID: 13734745
it seems to connect to the database... because if i try with a wrong password it says it cannot connect. Instead with right password/db/username i get that error when i click on the search button...

could it be that something is wrong in the database data or fields? i used your sql drop table code...

thanks!
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13735216
Something is going astray. After the mysql_connect do a var_dump($db) just to make sure it has a connection resource in it.
0
 

Author Comment

by:catalini
ID: 13737719
if i do that i get

Parse error: parse error in /home/httpd/vhosts/taunus.it/httpdocs/listino.php on line 7



where i wrote the var_dump($db)

i don't know if this can help you, i paste right now the sql dump of the database

# phpMyAdmin SQL Dump
# version 2.5.3
# http://www.phpmyadmin.net
#
# Host: localhost
# Generation Time: Apr 08, 2005 at 06:03 PM
# Server version: 3.23.58
# PHP Version: 4.3.9
#
# Database : `listino`
#

# --------------------------------------------------------

#
# Table structure for table `rooms`
#

DROP TABLE IF EXISTS `rooms`;
CREATE TABLE `rooms` (
  `roomtype` varchar(10) NOT NULL default '',
  `perioda` int(10) unsigned NOT NULL default '0',
  `periodb` int(10) unsigned NOT NULL default '0',
  `periodc` int(10) unsigned NOT NULL default '0',
  KEY `roomtype` (`roomtype`)
) TYPE=MyISAM;

#
# Dumping data for table `rooms`
#

INSERT INTO `rooms` (`roomtype`, `perioda`, `periodb`, `periodc`) VALUES ('A231', 12, 12, 23),
('B221', 51, 50, 50);
   
0
 

Author Comment

by:catalini
ID: 13737728
this is my listino.php file

....................................

<?
   if ($_GET['type'])
   {
     // search request submited
     $db = mysql_connect('localhost', 'accedi', 'mypassword') or die ('could not connect to DB'. mysql_error());
     var_dump($db)
     mysql_select_db($db, 'listino');

     $query = "select * from rooms where roomtype= '".$_GET['type']."';";

     $result = mysql_query($query);

     while ($row = mysql_fetch_assoc($result))
     {
        echo $row['roomtype']." ".$row['perioda']." ".$row['periodb']." ".$row['periodc']."<br>";
     }
     mysql_free_result($result);

     echo "<br><br><a href=".$_SERVER['PHP_SELF'].">Search again</a>";
     die();
   }

   echo "Room type:&nbsp;";
   echo "<form action=".$_SERVER['PHP_SELF']." method=GET>";
   echo "<select name=type>";
   echo "<option id=A231>A231</option>";
   echo "<option id=B221>B221</option>";
   echo "<option id=C251>C251</option>";
   echo "</select>";
   echo "&nbsp;";
   echo "<input type=submit value=Search>";
?>
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13738164
it needs a ; after it
0
 

Author Comment

by:catalini
ID: 13742774
now i get

resource(2) of type (mysql link)

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/httpd/vhosts/taunus.it/httpdocs/listino.php on line 7

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus.it/httpdocs/listino.php on line 13

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus.it/httpdocs/listino.php on line 17

0
 
LVL 6

Expert Comment

by:peyox
ID: 13744352
Ok, I found the problem and it is was mistake from previous post:

  mysql_select_db($db, 'listino');

should be:

  mysql_select_db('listino', $db);

Sorry :)
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13745311
Always the little things. I'm always getting needles and haystacks the wrong way around...
0
 

Author Comment

by:catalini
ID: 13781677
now it works!!! thanks


is there a way to paste the results at the right place in a table to have a nice formatting of the results?
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13781973
Just change how it's output inside your loop:

echo "<table>\n<tr><th>Room type</th><th>Period A</th><th>Period B</th><th>Period C</th></tr>\n";
while ($row = mysql_fetch_assoc($result))
     {
        echo "<tr><td>{$row['roomtype']}</td><td>{$row['perioda']}</td><td>{$row['periodb']}</td><td>{$row['periodc']}</td></tr>";
     }
echo "</table>\n";
0
 

Author Comment

by:catalini
ID: 13782300
works great! thanks to both! i will have to split some points :-)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question