ds2010630 2017-07-18 03:48
浏览 42
已采纳

在PHP中从sql数据库创建下拉列表

*The condition for "die" had been left out of my code by mistake when I copied it to the question. I put it back in.

I know this question might seem repetitive, but I have not found an answer in any of the other questions. I am trying to create a drop-down list based off a column in a database. I have tried two different ways and neither gave me correct results. Does anyone know a correct way of doing this?

The first way I saw in other StackOverflow answers (Fetching data from MySQL database to html drop-down list, Fetching data from MySQL database to html dropdown list). My code is below:

<?php
 $connect = mysql_connect('localhost', 'root');
 if ($connect == false)
   {
     die  ("Unable to connect to database<br>");
  }

$select = mysql_select_db('ViviansVacations');
if ($select == false)
   {
    die ("Unable to select database<br>");
  }
$query = "SELECT * FROM Destinations";
$result = mysql_query($query);
 ?>
 <select name="select1">
 <?php


while ($row = mysql_fetch_array($result))
{
echo "<option value='". $row['Europe'] ."'>" .$row['Europe'] ."</option>" ;
}
?>
</select>  

NetBeans sends me an error saying that "Text not allowed in element 'select' in this context".

The second way I tried:

<?php 
 $connect = mysql_connect('localhost', 'root'); 
  { 
   die  ("Unable to connect to database<br>");
  }
  $select = mysql_select_db('ViviansVacations'); 
  {
   die ("Unable to select database<br>");
  }
  $query = "SELECT * FROM Destinations";
  $result = mysql_query($query);
?>
<select name="select1">
<?php
  while ($line = mysql_fetch_array($result))
  {
?>
 <option value="<?php echo $line['Europe'];?>"> <?php echo 
 $line['field'];?> </option>
<?php
  }
?>
</select> 

This code did not produce any errors. However, inside the form were the opening php lines followed by an empty drop down box:

"); } $select = mysql_select_db('ViviansVacations'); { die ("Unable to select database "); } $query = "SELECT * FROM Destinations"; $result = mysql_query($query); ?>

  • 写回答

5条回答 默认 最新

  • dongsimu4422 2017-07-18 04:00
    关注

    There are couple of things, you to verify and correct.

    First of all, your database connection code, that doesn't seems to be correct. I didn't see any condition on which you are suppose to invoke die.

    $connect = mysql_connect('localhost', 'root');
    {
    die  ("Unable to connect to database<br>");
    }
    

    From the above code, below line will execute all the time

     die  ("Unable to connect to database<br>");
    

    You should correct your database connection code to below :

    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_close($link);
    

    You can refer mysql_connect for the usage.

    Also, verify your file extension is .php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题