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!
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!
ASKER
thanks! what should be the sql code to create the tables in the database? thanks again
ASKER
and how can i indicate which database should it use? (db password and so on)...
Firstly, install phpmyadmin: http://phpmyadmin.sourceforge.net/ and use it for managing your database without having to write code to do it.
Read any tutorial on using MySQL in PHP - there are hundreds!
http://www.php-mysql-tutorial.com/
http://www.createafreewebsite.net/phpmysql/phpmysql_introduction.html
http://dev.mysql.com/tech-resources/articles/ddws/
Beyond that, the PHP docs are great. These should be all the functions you need to learn about:
http://www.php.net/manual/en/function.mysql-connect.php
http://www.php.net/manual/en/function.mysql-select-db.php
http://www.php.net/manual/en/function.mysql-num-rows.php
http://www.php.net/manual/en/function.mysql-fetch-array.php
Read any tutorial on using MySQL in PHP - there are hundreds!
http://www.php-mysql-tutorial.com/
http://www.createafreewebsite.net/phpmysql/phpmysql_introduction.html
http://dev.mysql.com/tech-resources/articles/ddws/
Beyond that, the PHP docs are great. These should be all the functions you need to learn about:
http://www.php.net/manual/en/function.mysql-connect.php
http://www.php.net/manual/en/function.mysql-select-db.php
http://www.php.net/manual/en/function.mysql-num-rows.php
http://www.php.net/manual/en/function.mysql-fetch-array.php
ASKER
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
thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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/httpdoc s/listino2 .php on line 6
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus. it/httpdoc s/listino2 .php on line 12
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus. it/httpdoc s/listino2 .php on line 16
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.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus.
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus.
Have you changed this line?
mysql_select_db($db, 'mydatabase');
'mydatabase' - your database name here
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());
$db = mysql_connect('localhost',
ASKER
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!
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.
ASKER
if i do that i get
Parse error: parse error in /home/httpd/vhosts/taunus. it/httpdoc s/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);
Parse error: parse error in /home/httpd/vhosts/taunus.
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);
ASKER
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: ";
echo "<form action=".$_SERVER['PHP_SEL F']." 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 " ";
echo "<input type=submit value=Search>";
?>
..........................
<?
if ($_GET['type'])
{
// search request submited
$db = mysql_connect('localhost',
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'
die();
}
echo "Room type: ";
echo "<form action=".$_SERVER['PHP_SEL
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 " ";
echo "<input type=submit value=Search>";
?>
it needs a ; after it
ASKER
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/httpdoc s/listino. php on line 7
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus. it/httpdoc s/listino. php on line 13
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus. it/httpdoc s/listino. php on line 17
resource(2) of type (mysql link)
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/httpd/vhosts/taunus.
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus.
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/httpd/vhosts/taunus.
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 :)
mysql_select_db($db, 'listino');
should be:
mysql_select_db('listino',
Sorry :)
Always the little things. I'm always getting needles and haystacks the wrong way around...
ASKER
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?
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['per ioda']}</t d><td>{$ro w['periodb ']}</td><t d>{$row['p eriodc']}< /td></tr>" ;
}
echo "</table>\n";
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']
}
echo "</table>\n";
ASKER
works great! thanks to both! i will have to split some points :-)
<?
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'
die();
}
echo "Room type: ";
echo "<form action=".$_SERVER['PHP_SEL
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 " ";
echo "<input type=submit value=Search>";
?>