Link to home
Start Free TrialLog in
Avatar of catalini
catalini

asked on

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

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!

Avatar of peyox
peyox
Flag of Poland image

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>";
?>
Avatar of catalini
catalini

ASKER

thanks! what should be the sql code to create the tables in the database? thanks again
and how can i indicate which database should it use? (db password and so on)...
Avatar of Marcus Bointon
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
SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Have you changed this line?
mysql_select_db($db, 'mydatabase');

'mydatabase' - your database name here
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());
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!
Something is going astray. After the mysql_connect do a var_dump($db) just to make sure it has a connection resource in it.
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);
   
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>";
?>
it needs a ; after it
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

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 :)
Always the little things. I'm always getting needles and haystacks the wrong way around...
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?
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";
works great! thanks to both! i will have to split some points :-)