如何在选择菜单中放置-SELECT-

时间:2012-12-18 04:24:22

标签: php mysql

我的问题是我必须把SELECT的代码放入选择菜单,我尝试放入mysql但是如果我把单词SELECT我必须再次插入与国家名称相关的所有数据。我仍然用PHP开发自己希望你们帮助我 示例IN SELECT MENU BOX:

- 选择 -
AMERIKA

英国

KOREA

JAPAN

我从朋友那里运行这段代码之后就出现了Parse错误:在第411行的C:\ wamp ....中解析错误

       <?php
        $mysqlserver="localhost";
        $mysqlusername="root";
        $mysqlpassword="";
        $link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());

        $dbname = 'doktor';
        mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
        ?>
        <select id="Country" name="Country">
        <option value="">- SELECT - </option>
            <?php
         $cdquery="SELECT COUNTRY, ID FROM Country";
        $cdresult=mysql_query($cdquery) or die ("Query to get data from firsttable failed: ".mysql_error());
        while ($cdrow=mysql_fetch_array($cdresult)) {
            $cdTitle =$cdrow[COUNTRY];
            $cdId = $cdrow[COUNTRY];
             ?>
        <option value="<?php echo $cdId; ?>"><?php echo $cdTitle; ?></option>
         <?
            }
         ?>

2 个答案:

答案 0 :(得分:2)

将您的Select标记放在循环之外。

echo "<select>";
  while ( $cdrow = mysql_fetch_array( $cdresult ) ) {
    $cdTitle = $cdrow[COUNTRY];
    echo "<option>".$cdTitle."</option>";
  }
echo "</select>";

答案 1 :(得分:0)

我会这样做:

<?php
$mysqlserver = "localhost";
$mysqlusername = "root";
$mysqlpassword = "";
$link = mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());

$dbname = 'doktor';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
?>
<select id="Country" name="Country">
    <option value="">- SELECT - </option>
    <?php
    $cdquery = "SELECT COUNTRY FROM country";
    $cdresult = mysql_query($cdquery) or die ("Query to get data from first table failed: ".mysql_error());
    while ($cdrow = mysql_fetch_assoc($cdresult)) {
        $cdTitle = $cdrow['COUNTRY'];
    ?>
        <option value="<?php echo $cdTitle; ?>"><?php echo $cdTitle; ?></option>
    <?php
    }
    ?>
</select>

如果您使用mysql_fetch_array(),则应使用$cdrow[0],或者如果您使用mysql_fetch_assoc(),则应使用$cdrow['COUNTRY']。否则,你会收到通知。