Jump to content

Populating Drop Down Menu's with MySQL Data


zachatk1

Recommended Posts

Alright so I created a MySQL database that has 5 tables each named a brand of a dirt bike.

 

In each table their are 2 fields, one INDEX_ID and MODELS.

 

Under that for rows I have every model bike named.

 

A quick question before I get onto what I want to do:

 

Can data be added underneath the rows?

 

For example, users will be able to submit information about each bike model. If each bike model is a row, can there be a category past that row or do I need to make each field a model name and just have a ton of fields and have the rows be the information users submit.

 

To make it easier to understand I'll post the SQL code for the brand Honda:

 

CREATE TABLE `Honda` (

  `INDEX_ID` int(3) NOT NULL auto_increment,

  `MODELS` varchar(20) collate latin1_general_ci NOT NULL,

  PRIMARY KEY  (`INDEX_ID`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=23 ;

 

--

-- Dumping data for table `Honda`

--

 

INSERT INTO `Honda` VALUES(1, 'CR85');

INSERT INTO `Honda` VALUES(2, 'CR125');

INSERT INTO `Honda` VALUES(3, 'CR250');

INSERT INTO `Honda` VALUES(4, 'CRF100');

INSERT INTO `Honda` VALUES(5, 'CRF150');

INSERT INTO `Honda` VALUES(6, 'CRF230');

INSERT INTO `Honda` VALUES(7, 'CRF250X');

INSERT INTO `Honda` VALUES(8, 'CRF250R');

INSERT INTO `Honda` VALUES(9, 'CRF450X');

INSERT INTO `Honda` VALUES(10, 'CRF450R');

INSERT INTO `Honda` VALUES(11, 'CRF50');

INSERT INTO `Honda` VALUES(12, 'CRF70');

INSERT INTO `Honda` VALUES(13, 'CRF80');

INSERT INTO `Honda` VALUES(14, 'XR650');

INSERT INTO `Honda` VALUES(15, 'CR500');

INSERT INTO `Honda` VALUES(16, 'XR100');

INSERT INTO `Honda` VALUES(17, 'XR200');

INSERT INTO `Honda` VALUES(18, 'XR250');

INSERT INTO `Honda` VALUES(19, 'XR400');

INSERT INTO `Honda` VALUES(20, 'XR50');

INSERT INTO `Honda` VALUES(21, 'XR70');

INSERT INTO `Honda` VALUES(22, 'XR80');

 

Anyway I still need to figure out how to have this under a form that a user can use to select the bike they want to submit information about. A code like this perhaps?:

 

<?
$connection = mysql_connect("localhost","user","pass");
$fields = mysql_list_fields("dbname", "table", $connection);
$columns = mysql_num_fields($fields);
echo "<form action=page_to_post_to.php method=POST><select name=Field>";
for ($i = 0; $i < $columns; $i++) {
echo "<option value=$i>";
echo mysql_field_name($fields, $i);
}
echo "</select></form>";
?>

 

Thanks.

Alright so I created a MySQL database that has 5 tables each named a brand of a dirt bike.

 

In each table their are 2 fields, one INDEX_ID and MODELS.

 

Under that for rows I have every model bike named.

 

A quick question before I get onto what I want to do:

 

Can data be added underneath the rows?

 

For example, users will be able to submit information about each bike model. If each bike model is a row, can there be a category past that row or do I need to make each field a model name and just have a ton of fields and have the rows be the information users submit.

 

To make it easier to understand I'll post the SQL code for the brand Honda:

 

CREATE TABLE `Honda` (

  `INDEX_ID` int(3) NOT NULL auto_increment,

  `MODELS` varchar(20) collate latin1_general_ci NOT NULL,

  PRIMARY KEY  (`INDEX_ID`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=23 ;

 

--

-- Dumping data for table `Honda`

--

 

INSERT INTO `Honda` VALUES(1, 'CR85');

INSERT INTO `Honda` VALUES(2, 'CR125');

INSERT INTO `Honda` VALUES(3, 'CR250');

INSERT INTO `Honda` VALUES(4, 'CRF100');

INSERT INTO `Honda` VALUES(5, 'CRF150');

INSERT INTO `Honda` VALUES(6, 'CRF230');

INSERT INTO `Honda` VALUES(7, 'CRF250X');

INSERT INTO `Honda` VALUES(8, 'CRF250R');

INSERT INTO `Honda` VALUES(9, 'CRF450X');

INSERT INTO `Honda` VALUES(10, 'CRF450R');

INSERT INTO `Honda` VALUES(11, 'CRF50');

INSERT INTO `Honda` VALUES(12, 'CRF70');

INSERT INTO `Honda` VALUES(13, 'CRF80');

INSERT INTO `Honda` VALUES(14, 'XR650');

INSERT INTO `Honda` VALUES(15, 'CR500');

INSERT INTO `Honda` VALUES(16, 'XR100');

INSERT INTO `Honda` VALUES(17, 'XR200');

INSERT INTO `Honda` VALUES(18, 'XR250');

INSERT INTO `Honda` VALUES(19, 'XR400');

INSERT INTO `Honda` VALUES(20, 'XR50');

INSERT INTO `Honda` VALUES(21, 'XR70');

INSERT INTO `Honda` VALUES(22, 'XR80');

 

Anyway I still need to figure out how to have this under a form that a user can use to select the bike they want to submit information about. A code like this perhaps?:

 

<?
$connection = mysql_connect("localhost","user","pass");
$fields = mysql_list_fields("dbname", "table", $connection);
$columns = mysql_num_fields($fields);
echo "<form action=page_to_post_to.php method=POST><select name=Field>";
for ($i = 0; $i < $columns; $i++) {
echo "<option value=$i>";
echo mysql_field_name($fields, $i);
}
echo "</select></form>";
?>

 

Thanks.

 


<?PHP

$con=mysql_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD);
mysql_select_db(DB_DATABASE, $con);

$sql = "SELECT field1 FROM table";

$result = mysql_query($sql,$con);
?>

<form>
	<select>

		<?PHP

		while($row = mysql_fetch_assoc($result))
			{
				echo "<option>" .$row['field1]. "</option>";
			}

		?>

	</select>
</form>

 

Something like this would work.

make sure you replace the instances of "field1" with the column titles of your DB (INDEX_ID & MODEL).

 

Also, you might want to pass the ID into the form

echo "<option value=\"" . $row['INDEX_ID'] . "\">" . $row['MODEL'] . "</option>" . PHP_EOL;

Still having issues. I viewed the page source and discovered something that wasn't displaying on the webpage. It says:

 

PHP Error Message

 

Warning:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a2986016/public_html/modform.php on line 22

 

What does it mean?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.