Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need PHP Select records into dropdown box help

Status
Not open for further replies.

malibu65k

Programmer
Sep 27, 2004
131
US
I have php code to select records from a table in mysql. I have a dropdown box that isn't filling in with the records. Not sure what I am doing wrong. Thank you in advance for any help you can provide.

Here is my code...

<html>
<body>
<head>
<title>Search for a Pattern</title>
</head>
<body bgcolor="#ffffff">

<h2>Search for a Pattern</h2>

<form action="action" method="post">
<select name="option">

<?php

$user = "Admin";
$host = "LocalHost"
$password = ""
$dbName = "mypatterns"

mysql_connect($host, $user, $password) OR DIE( "Unable to connect to database");

mysql_select_db($dbName);

$sql = "SELECT PatternNo FROM patterns";
$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_array($result))

{
echo '<option value="' . $row['PatternNo'] . '">' . $row['PatternNo'] . '</option>';
}

?>
</select>

</form>
 
First, mysql_ functions are a bad idea; try switching to mysqli_ for better sanitation and being type safe.

The die(...) function should be lowercase in both instances.

besides that, what error(s) are you getting?

[plug=shameless]
[/plug]
 
mysql_ functions are a bad idea

I disagree.

The die(...) function should be lowercase in both instances.

there is no need for this. functions in php are case insensitive.

@OP
the errors that I can see are that your variable assignments are not terminated by a semicolon.
Code:
$user = "Admin";  
$host = "localhost" ;
$password = "" ;
$dbName = "mypatterns";

make sure that the case of your user name and password is correct.

it is a good idea when debugging to add the following to the start of your file
Code:
error_reporting(E_ALL);
ini_set('display_errors', true);

adding some debug to your mysql_* calls is a good idea

Code:
mysql_select_db($dbName) or die (mysql_error());

perhaps use mysql_fetch_assoc rather than _array as you are referencing the columns by their name.

Code:
while ($row = mysql_fetch_assoc($results)){

}

other things to be aware of:

1. might the patternno have any quotes in it? if so the value attribute might be breaking.
2. you might consider running htmlspecialchars on the value of PatternNo when in the text part of the <option> tag
3. the <select> element must be terminated
Code:
echo '</select>';
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top