Jump to content

Archived

This topic is now archived and is closed to further replies.

V-Man

Populate Dropdowns

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]

Share this post


Link to post
Share on other sites
[code]<select name=""> [/code]
That provides no method for passing the form variable anywhere. I suspect that what you want is:
[code]<select name="title">[/code]

Share this post


Link to post
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?

Share this post


Link to post
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


Share this post


Link to post
Share on other sites

×

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.