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
Share on other sites

Nice spot, but thats just naming the HTML form element. With my current setup, I am not able to pass info into the select list to populate it because of my SQL. It's not finding something Im thinking... Ideas anybody?
Link to comment
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
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.