I want to use the following code to populate a drop-down list with all of the customer types:
<select name="type" id="type" class="neutral">
<?php // SQL QUERY TO RETRIEVE EVERY TYPE OF CUSTOMER
$sql = "SELECT CUST_TYPE FROM `CUSTOMER` GROUP BY `CUST_TYPE`";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){echo '<option value="'.$row.'">'.$row.'</option>';}
?>
The query works in phpMyAdmin; it gives the correct output (corporate, other, school, sports) but in the webpage it displays a drop-down list with 4 options, all containing the word "array." Please help!
Try
while($row = mysql_fetch_assoc($result)){
echo '<option value="'.$row['CUST_TYPE'].'">'.$row['CUST_TYPE'].'</option>';
}
Related
I would like to create a multi-select drop down list of countries in my form which lets users choose more than one country and also provides the ability to remove the item they selected, if needed.
I could create a drop down list which extracts the name of the countries from a mysql table, but it lets users only select one country. I searched a lot for multi-select drop down but none of the samples that I have found get data from mysql. They had only a few options which could easily write with option value like How to access the values selected in the multiselect dropdown list in PHP?
This is my code through which only one item can be selected:
<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('imdb');
$sql2 = "SELECT country FROM countries";
$result2 = mysql_query($sql2);
echo "<select name='country'>";
while ($row = mysql_fetch_array($result2)) {
echo "<option value='" . $row['country'] . "'>" . $row['country'] . "</option>";
}
echo "</select>";
?>
Finally, I found what I was looking for. So I share it since might be useful for those with similar question.
First, as Marc said, I had to add multiple like below:
<select name="countrylist[]" multiple="multiple">
and this is the html line that I was searching for :
<option value="<?php echo $row['country'];?>"> <?php echo $row['country'];?> </option>
where country is the name of the related field in my database.
it might be worth saying that in order to insert the result in database (e.g table name = "test", field name = "q5":
<?php
mysql_connect("hostname", "user name", "user password") or die(mysql_error());
mysql_select_db("db name") or die(mysql_error());
$q5 = implode(',', $_POST['countrylist']);
if(isset($_POST['submit']))
{
$query="INSERT INTO test (q5) values ('". $q5 ."')";
mysql_query($query) or die (mysql_error() );
}
?>
It worked for me and hope will be useful for others.
I have a PHP form that is collecting information and writing it to a database and it is working correctly. I have one field that is a select to choose entries from an existing table. I would like that result to be inserted into a different table but can't get the insert into to work. I would like to insert the FacilityName field chosen in the select to table2.
$result = mysql_query("SELECT * FROM facility");
print "<select name=\"Id\" > \n";
while ($row = mysql_fetch_array($result)){
$Id = $row['Id'];
$FacilityName = $row['FacilityName'];
print "<option value=$Id>$FacilityName\n";
I am currently working on a school project. My goal is to develop a dynamic web page that allows people to retrieve data from a database. I want to create a few drop down boxes that allows users to narrow down the data from my database I have created.
For example, I have "year" as one column in my database because I have gathered data from multiple years. I would like to establish a way for users to select a specific year by using an HTML drop down box. How exactly do I go about coding something like this using PHP and my database?
Here is my code so far, but I can't seem to get anywhere with this.
<select name='year'>
<?php
$query = "select distinct year from test order by year";
$result = $result->query($query);
while ($row = $result->fetch_assoc()) {
echo "<option value='".$row->year."'>".$row->year."</option>";
}
?>
</select>
With this code, I am getting a drop down box, but no choices are given. It is blank. Any ideas? I would appreciate any help.
fetch_assoc() return the associative array not an object,should be accessed like array
$query = "select distinct year from test order by year";
$result = $result->query($query);
while ($row = $result->fetch_assoc()) {
echo "<option value='".$row['year']."'>".$row['year']."</option>";
}
<select name='year'>
<?php
$query = "select distinct year from test order by year";
$result = $result->query($query);
while ($row = $result->fetch_object()) {
echo "<option value='".$row->year."'>".$row->year."</option>";
}
?>
</select>
To access the result as objects you should use fetch_object.
So your loop should change as below:
while ($row = $result->fetch_object()) {
Or use
$sql=mysql_query("select distinct year from test order by year");
while ($row = mysql_fetch_assoc($sql)) {
echo "<option value='".$row["year"]."'>".$row["year"]."</option>";
}
I'm having a input form that asks for a location. The locations are stored in a mysql db and have an id (colomn: id and column: location).
I have a drop down menu that is generated from those records in the db:
<select name="location">
<?php
$query="SELECT location FROM locations";
$result=mysql_query($query) or die;
while ($row=mysql_fetch_array($result)) {
$location=$row['location'];
echo "<option>$location</option>";
?>
</select>
This all works. When the form is sumbitted, I obviously get a POST[location] for example "Belgium".
Let's say Belgium is the location in my db and has id 5, how can I return the ID as the POST variable from the dropdown box, instead of the location. Ofcourse I want the dropdown to show the locations, and not the ID's.
Thanks in advance!
Each option can take a value and show another string so use value="my_value" for each option inside the select tag
<select name="location">
<?php
$query="SELECT id, location FROM locations";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)) {
echo "<option value=\"" . $row['id'] . "\">" . $row['location'] . "</option>";
}
?>
</select>
now your POST['location'] will contain the db id for selected location
if you change the SQl query to include the ID of the location,
you can assign that value to the dropdown selected value.
<select name="location">
<?php
$query="SELECT id, location FROM locations";
$result=mysql_query($query) or die;
while ($row=mysql_fetch_array($result)) {
$location=$row['location'];
$id = $row['id'];
echo "<option value='".$row['id']."'>".$location."</option>";
?>
</select>
select ID from locations;
$ID=$row['ID'];
Replace ID by the ID-name of your column ofcourse.
If you say select * from locations you can do something like this:
$row['anyCOLUMNNAME']
You can choose any column you like and use that information from that particular row.
<select name="location">
<?php
// select columns you need, separate by , (comma)
$query = "SELECT `column1`, `column2` FROM `locations`;";
$result = mysql_query($query) or die;
while ($row = mysql_fetch_array($result)) {
// selected columns become accessible in $row array
// value attribute needs to be escaped here
echo '<option value="', htmlentities($row['column1']),'">',
htmlentities($row['column2']), '</option>'; // escape label too
// <option> does not accept HTML in label so it should be escaped
} // done!
?>
</select>
^ this (read comments for explanations)
I do have table table1 with two columns say name and details when I search by details there were multiple name listing. I want to retrieve that name list and display to a webpage using PHP. how can I do that?
the most basic page.php:
<?php
$sql = mysql_query('SELECT name FROM table1 WHERE details = "something"');
$result = array();
while($row = mysql_fetch_assoc($sql)){
array_push($result,$row['name']);
echo $row['name'].'<br />';
}
print_r($result);