Jump to content

Populate Dropdowns


V-Man

Recommended Posts

Sure, it's been asked before, but I still don't get it. Can I get some help?

[code]
<?php

define('IN_MINVERA', TRUE);

$type = $_GET['type'];
include("includes/db.php");
db_connect();
  
$sql = "SELECT title FROM articles WHERE type = $type";

$result = mysql_query($sql)
          or die('Query failed. ' . mysql_error());

if (mysql_num_rows($result) == 1)
{
    while ($row = mysql_fetch_array($result))
    {        
    $title = $row['title'];
    ?>
    <select name="">
    <?php echo("<option value='$title'>$title</option>"); ?>
    </select>
    <?php
    }
}
    
?>
[/code]

Thats the code. When ?type=blablabla (happens to be om_article) I get the following error:
Query failed. Unknown column 'om_article' in 'where clause'

SQL table is set up as following:
[code]
CREATE TABLE `articles` (
  `article_id` tinyint(10) unsigned NOT NULL auto_increment,
  `title` varchar(200) default '0',
  `type` varchar(200) default '0',
  `dop` varchar(20) default '0',
  `content` text,
  PRIMARY KEY  (`article_id`)
) TYPE=MyISAM AUTO_INCREMENT=29;
[/code]
Link to comment
https://forums.phpfreaks.com/topic/8858-populate-dropdowns/
Share on other sites

first you should do is put single quotes around your strings in query


[code]$sql = "SELECT title FROM articles WHERE type = '$type'";[/code]

Second if you are populating a dropdown menu why would there be only one result from the query
[code]if (mysql_num_rows($result) == 1)[/code]

Should be
[code]if (mysql_num_rows($result) > 0)[/code]

Third your select tags need to be before the loop

[code]echo "<select name=\"something\">";
    while ($row = mysql_fetch_array($result))
    {        
    $title = $row['title'];
    print '<option value="'.$title.'">'.$title.'</option>;
}
   echo "</select>";
}[/code]

Ray


Link to comment
https://forums.phpfreaks.com/topic/8858-populate-dropdowns/#findComment-32510
Share on other sites

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.