ds2010630
ds2010630
2017-07-18 03:48

在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 dongsimu4422 4年前

    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

    点赞 评论 复制链接分享
  • dongxian4531 dongxian4531 4年前

    You might want to try to use this ..The dept_id and the description u have to change according to your database .Hope this will help you

     <?php 
      $sql = mysql_query("SELECT dept_id ,description FROM department 
      ORDER BY dept_id ASC");
      db_select($sql,"dept_id",$dept_id,"","-select Department-","","");
      ?>
    
    点赞 评论 复制链接分享
  • dongyou4411 dongyou4411 4年前

    There could be few issues:

    • Use mysqli_connect instead of mysql_connect because mysql is depreciated
    • Make 3rd parameter as empty `mysqli_connect('localhost', 'root', '');//as per standards
    • Debug your code for example: (print_r($connect), execute your query in phpmyadmin, print_r($result) and then in loop print_r($row).

    At end, I am sure you will get your desired result and also you will know in detail that what was happening.

    点赞 评论 复制链接分享
  • dongluni0568 dongluni0568 4年前

    Mainly your problem is database connection. Try this -

    <?php
    mysql_connect("localhost", "root", "") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("your_db");
    
    $result = mysql_query("SELECT * FROM your_table");
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        print_r($row);
        echo "<br>";
    }
    
    mysql_free_result($result);
    ?>
    
    点赞 评论 复制链接分享
  • dozpox8752 dozpox8752 4年前

    So there are many problems with your approach. First of all you are using a deprecated mysql_* functions which is bad idea and second you are not debugging your database connection properly :

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

    In above code the die statement will always execute stopping further execution.

    Also make sure the database ViviansVacations and table Destinations and column Europe exists with correct names(Follow standards and try to use all small letters for database/table/column naming)

    The correct mysqli_* approach is(tested locally and the select box forms correctly) :

    <?php
    $db = 'ViviansVacations';
    
    $mysqli = new mysqli('localhost', 'root', '', $db);
    
    if($mysqli->connect_error) 
      die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
    
    
    $query = "SELECT * FROM Destinations";
    $result = mysqli_query($mysqli, $query);
    
    ?>
    
    <select name="select1">
    <?php
    while ($row = mysqli_fetch_array($result)) {
        echo "<option value='" . $row['Europe'] . "'>" . $row['Europe'] . "</option>";
    }
    ?>
    </select>  
    
    点赞 评论 复制链接分享

为你推荐