Jump to content

Retrieve query into drop down


englishcodemonkey

Recommended Posts

OK so i'm trying to retrieve data from my mysqli database and display in a drop down box.  My database is shown below:

 

Table structure for table hosta

 

Field                          Type Null Default

hosta_id                 int(11)  Yes NULL

hosta_name varchar(50) Yes

hybridizer           varchar(50) Yes NULL

size             varchar(10) Yes NULL

description varchar(1000) Yes NULL

price             float(5,2) Yes NULL

 

and my code i have so far is:


  <?php

require_once('conn.php');

echo '<select name='hosta' id='hosta'>
<option> -- select -- </option>';

$q = "SELECT hosta_name FROM hosta";
$r = mysqli_query($q, $dbc);

while($row=mysqli_fetch_array($r, MYSQLI_ASSOC)){
echo '<option value="'.$row['hosta_name'].'">'.$row['hosta_name'].'</option>';
}
echo '</select>';

?>

 

at the moment nothing is showing in my website frame?? Any ideas? Thanks guys

Link to comment
Share on other sites

The error is on the line that shows echo '<select name='hosta' id='hosta'>

you need to either replace the single quotes around hosta with double quotes, or escape the single quotes with a backslash. By putting single quotes, you're basically ending the string at "=", putting some text that the compiler can't understand('hosta') then starting the string back again but with no echo, and then repeating. Backslashes are the key to having quotes within echo statements(and others)

Link to comment
Share on other sites

So creating the drop down box works!! But i need to be able to create a submit button so that once the name is selcted the form posts this information to 'desc.php'.

 

This is my code now:

 

<?php
require_once('conn.php');

$q = "SELECT hosta_name FROM hosta WHERE hosta_name LIKE 'A%' ORDER BY hosta_name";

$r = @mysqli_query ($dbc, $q);


if ($r) {

echo '<select name="hosta" id="hosta">
	<option><b> Select a Hosta </b></option>';


while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC))  {
	echo ' <option value="'.$row['hosta_name'].'">'.$row['hosta_name'].'</option>';
	}
	echo '</select>';


			}
?>

 

Any ideas?

THANKS!

Link to comment
Share on other sites

Well, when you have a form, there are 3 critical parts to successfully posting it. Action, method, and submit.

 

You will need, before any form elements are displayed: <form action=PAGE.php method="POST">

 

method="GET" is also an option, but I'd only do it for something like a search page

 

then you need <input type=submit value="Submit">

 

 

also, close the form with </form> It's not required if that's the only form on your page, but if you have 2 separate forms, you will need it so the browser(and processing page) can know which page to go to and which input elements to process.

 

and the string in value can be whatever you want. There are other ways to display and edit the way submit buttons look and work, but I suggest getting your form to correctly submit before messing around any further with the way it looks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.