php select option value need help

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
User avatar
klevz
Forum Newbie
Posts: 5
Joined: Mon Sep 08, 2008 8:54 pm
Location: Philippines
Contact:

php select option value need help

Post by klevz »

guys please help me in getting value from my database and display it on option drop down menu,, thanx,,

this code wont work.... :(
<HTML>
<HEAD>
<TITLE>Vinyl Dealers: search</TITLE>
</HEAD>

<BODY>
<H2>Search for a shop</H2>
<BR>

<FORM ACTION="bygenre.php" METHOD="POST">
Which genre of music are you looking for?
<BR>
<SELECT NAME="genre">
<?
// connect to database and choose vinyldealers database
$host = "localhost";
$user = "root";
$pass = "";
$database = "klevz";

mysql_connect ($host, $user, $pass) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());

// select all records from genres table
$query = "SELECT * FROM genres";
$result = mysql_query($query);

// loop through all records returned by the query
while ($record = mysql_fetch_assoc($result)) {

// for each record, add an option to the select field on the page
echo "<OPTION VALUE = ".$record['genreID'].">".$record['name'];
}
?>

</SELECT>
<BR>
<INPUT TYPE=SUBMIT VALUE="search">
</FORM>
</BODY>
</HTML>
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: php select option value need help

Post by 8ennett »

klevz wrote:guys please help me in getting value from my database and display it on option drop down menu,, thanx,,

this code wont work.... :(

Code: Select all

[color=#000080]<HTML>
<HEAD>
 <TITLE>Vinyl Dealers: search</TITLE>
</HEAD>
 
<BODY>
 <H2>Search for a shop</H2>
 <BR>[/color]
 <FORM ACTION="bygenre.php" METHOD="POST">
  Which genre of music are you looking for?
  <BR>
  [color=#BF0000]<SELECT NAME="genre">
  <?
    // connect to database and choose vinyldealers database
    $host = "localhost";
    $user = "root";
    $pass = "";
    $database = "klevz";
    
    mysql_connect ($host, $user, $pass) or die (mysql_error());
    mysql_select_db($db) or die (mysql_error());
    
    // select all records from genres table
    $query = "SELECT * FROM genres";
    $result = mysql_query($query);
 
    // loop through all records returned by the query
    while ($record = mysql_fetch_assoc($result)) {
 
        // for each record, add an option to the select field on the page
        echo "<OPTION VALUE = ".$record['genreID'].">".$record['name'];
    }
  ?>[/color]
 [color=#000080] </SELECT>
  <BR>
  <INPUT TYPE=SUBMIT VALUE="search">
 </FORM>
</BODY>
</HTML> [/color][/quote]
Well, for starters your <?php starting tag is incorrect!!!
User avatar
8ennett
Forum Commoner
Posts: 63
Joined: Sat Sep 06, 2008 7:05 am

Re: php select option value need help

Post by 8ennett »

also, create a new file on your server called 8ennettinfo.php, make sure the source code is EXACTLY;

<?php
info();
?>

If there is nowhere it says MySQL on that page when you load it up through either http://localhost or the directory where you uploaded the files (ensuring the php server picks up and outputs the correct html) then MySql has not been setup properly. Many tutorials online to search for this, just go ahead, it's easy!
Cut
Forum Commoner
Posts: 39
Joined: Sat Aug 23, 2008 8:01 pm

Re: php select option value need help

Post by Cut »

Don't be vague. "Won't work" doesn't help.

Line 31 is wrong. It should be:

Code: Select all

echo "<OPTION VALUE = ".$record['genreID'].">".$record['name']."</OPTION>";
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: php select option value need help

Post by starram »

Cut is right, you haven't closed the </option> tag.
User avatar
klevz
Forum Newbie
Posts: 5
Joined: Mon Sep 08, 2008 8:54 pm
Location: Philippines
Contact:

Re: php select option value need help

Post by klevz »

hi guys thanks for your replies but still it doesn't work.. i cant still pick the data's from my database and show it on drop down menu...

here's my sql statement

Code: Select all

-- phpMyAdmin SQL Dump
-- version 2.11.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Sep 09, 2008 at 05:37 AM
-- Server version: 5.0.51
-- PHP Version: 5.2.5
 
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
 
--
-- Database: `vinyldealers`
--
 
-- --------------------------------------------------------
 
--
-- Table structure for table `feedback`
--
 
CREATE TABLE IF NOT EXISTS `feedback` (
  `feedbackID` tinyint(3) unsigned NOT NULL auto_increment,
  `user` varchar(50) NOT NULL default '',
  `email` varchar(50) NOT NULL default '',
  `spam` tinyint(4) NOT NULL default '0',
  `comments` text NOT NULL,
  PRIMARY KEY  (`feedbackID`),
  UNIQUE KEY `feedbackID` (`feedbackID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
 
--
-- Dumping data for table `feedback`
--
 
 
-- --------------------------------------------------------
 
--
-- Table structure for table `genres`
--
 
CREATE TABLE IF NOT EXISTS `genres` (
  `genreID` tinyint(3) unsigned NOT NULL auto_increment,
  `name` char(20) NOT NULL default '0',
  PRIMARY KEY  (`genreID`),
  UNIQUE KEY `genreID` (`genreID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
 
--
-- Dumping data for table `genres`
--
 
INSERT INTO `genres` (`genreID`, `name`) VALUES
(1, 'Techno'),
(2, 'Trance'),
(3, 'Drum & Bass'),
(4, 'Hiphop'),
(5, 'Breaks'),
(6, 'House'),
(7, 'Hardcore'),
(8, 'US Garage'),
(9, 'UK Garage'),
(10, 'Funk'),
(11, 'Electro'),
(12, 'Gabba'),
(13, 'Reggae'),
(14, 'Dancehall'),
(15, 'Disco'),
(16, 'Dub');
 
-- --------------------------------------------------------
 
--
-- Table structure for table `shopgenres`
--
 
CREATE TABLE IF NOT EXISTS `shopgenres` (
  `shopID` tinyint(3) unsigned NOT NULL default '0',
  `genreID` tinyint(3) unsigned NOT NULL default '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
--
-- Dumping data for table `shopgenres`
--
 
INSERT INTO `shopgenres` (`shopID`, `genreID`) VALUES
(1, 1),
(1, 2),
(1, 3),
(1, 5),
(1, 7),
(1, 11),
(2, 1),
(2, 3),
(2, 5),
(2, 6),
(2, 7),
(2, 8),
(2, 9),
(2, 11),
(3, 3),
(3, 4),
(3, 7),
(3, 9),
(3, 13),
(3, 14),
(4, 1),
(4, 2),
(4, 5),
(4, 6),
(4, 3),
(4, 8),
(5, 3),
(5, 4),
(5, 7),
(5, 9),
(5, 10),
(5, 14),
(6, 4),
(6, 10),
(2, 15),
(2, 10),
(7, 1),
(7, 2),
(7, 6),
(8, 1),
(8, 3),
(8, 7),
(8, 2),
(8, 12),
(9, 6),
(9, 15),
(10, 10),
(10, 5),
(10, 11),
(10, 3),
(10, 9),
(3, 16),
(5, 16);
 
-- --------------------------------------------------------
 
--
-- Table structure for table `shops`
--
 
CREATE TABLE IF NOT EXISTS `shops` (
  `shopID` tinyint(3) unsigned NOT NULL auto_increment,
  `name` varchar(40) NOT NULL default '',
  `address1` varchar(40) default NULL,
  `address2` varchar(40) default NULL,
  `suburb` varchar(20) default NULL,
  `phone` varchar(8) NOT NULL default '',
  `fax` varchar(8) NOT NULL default '',
  `email` varchar(40) NOT NULL default '',
  `website` varchar(40) NOT NULL default '',
  `comments` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`shopID`),
  UNIQUE KEY `shopID` (`shopID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
 
--
-- Dumping data for table `shops`
--
 
INSERT INTO `shops` (`shopID`, `name`, `address1`, `address2`, `suburb`, `phone`, `fax`, `email`, `website`, `comments`) VALUES
(1, 'Bleep Bleep Records', '14 Industrial Ave', '', 'Troughton', '65465465', '64351231', 'bleepbleep@freeemail.com', '', 'Interesting UV-glow clubwear also on sale'),
(2, 'Hi Fly Sounds', '15 Outersdale Road', '', 'Imblendon', '64231615', '', 'hiflier@hotmail.com', '', ''),
(3, 'Tuff Tune', '13 Yard St', '', 'Troughton', '64321654', '64351684', 'tufftune@yahoo.com', '', 'Specialist t''ing ..'),
(4, 'Deeper Beat Records', '53 Blempton Ave', '', 'Durringvale', '64231654', '64615644', 'info@deeperbeat.com.au', 'www.deeperbeat.com.au', ''),
(5, 'Riddim Records', '37 Dorset Road', '', 'Hellington', '64234516', '', 'manager@riddim.com.au', 'www.riddim.com.au', 'Rudeboy riddims'),
(6, 'Phat Bass Music', '91 Bevelled Road', 'Long Park', 'Sirdale', '64321684', '64351568', 'phatbass@mymail.com', '', 'Loud flavas'),
(7, 'Minimalism', '24 Crane St', '', 'Hellington', '64321684', '64321684', 'us@minimalism.com.au', 'www.minimalism.com.au', 'Seriously stripped-down techno'),
(8, 'Explosive Records', '71 Bang St', '', 'Troughton', '64321684', '64651684', 'bang@hotmail.com', '', ''),
(9, 'Fab Sounds', '21 Enders Lane', '', 'Imbledon', '64231654', '64654651', 'tony@fabsound.com.au', 'www.fabsound.com.au', 'Gorgeous toons daarling!'),
(10, 'Freaky Beat', '85 Ungoliant Ave', 'Wyrd Hills', 'Trippiton', '64321654', '', 'str@oddside.com.au', '', 'Very, very odd.');
 
here's my .php file..

Code: Select all

<HTML>
<HEAD>
 <TITLE>Vinyl Dealers: search</TITLE>
</HEAD>
 
<BODY>
 <H2>Search for a shop</H2>
 <BR>
 <FORM ACTION="bygenre.php" METHOD="POST">
  Which genre of music are you looking for?
  <BR>
  
  <SELECT NAME='genre'>
  <?php
    // connect to database and choose vinyldealers database
    $host = "localhost";
    $user = "root";
    $pass = "";
    $database = "vinyldealers";
    
    mysql_connect ($host, $user, $pass) or die (mysql_error());
    mysql_select_db($db) or die (mysql_error());
    
    // select all records from genres table
    $query = "SELECT * FROM genres";
    $result = mysql_query($query);
 
    // loop through all records returned by the query
    while ($record = mysql_fetch_assoc($result)) {
 
        // for each record, add an option to the select field on the page
        echo "<OPTION VALUE = ".$record['genreID'].">".$record['name']."</OPTION>";
    }
  ?>
  </SELECT>
  <BR>
  <INPUT TYPE=SUBMIT VALUE="search">
 </FORM>
</BODY>
</HTML>
thanks again.. [need help]
Post Reply