Jump to content


Photo

Populate Dropdowns


  • Please log in to reply
3 replies to this topic

#1 V-Man

V-Man
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationVermont

Posted 02 May 2006 - 12:22 AM

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

<?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
    }
}
    
?>

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:
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;

rtfm :)

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 02 May 2006 - 12:28 AM

<select name="">
That provides no method for passing the form variable anywhere. I suspect that what you want is:
<select name="title">

Legend has it that reading the manual never killed anyone.
My site

#3 V-Man

V-Man
  • Members
  • PipPip
  • Member
  • 28 posts
  • LocationVermont

Posted 02 May 2006 - 12:34 AM

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?
rtfm :)

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 02 May 2006 - 02:44 AM

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


$sql = "SELECT title FROM articles WHERE type = '$type'";

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

Should be
if (mysql_num_rows($result) > 0)

Third your select tags need to be before the loop

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

Ray







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users