简体   繁体   English

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

[英]Creating Dropdown list from sql database in PHP

*The condition for "die" had been left out of my code by mistake when I copied it to the question. *当我将“ die”的条件复制到问题时,它被错误地排除在我的代码之外。 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 ). 我在其他StackOverflow答案中看到的第一种方式( 从MySQL数据库获取数据到html下拉列表从MySQL数据库获取数据到html下拉列表 )。 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". NetBeans向我发送一条错误消息,提示“在此上下文中,元素'select'中不允许使用文本”。

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: 但是,在表单内部是开头的php行,后跟一个空的下拉框:

"); } $select = mysql_select_db('ViviansVacations'); { die ("Unable to select database "); } $query = "SELECT * FROM Destinations"; $result = mysql_query($query); ?> “);} $ select = mysql_select_db('ViviansVacations'); {die(”无法选择数据库“);} $ query =” SELECT * FROM Destinations“; $ result = mysql_query($ query);?>

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 : 首先,您正在使用不mysql_*使用的mysql_*函数,这是个坏主意,其次,您没有正确调试数据库连接:

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

In above code the die statement will always execute stopping further execution. 在上面的代码中,die语句将始终执行以停止进一步执行。

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) 还要确保数据库ViviansVacations和表Destinations以及Europe列具有正确的名称(遵循标准,并尝试对数据库/表/列命名使用所有小写字母)

The correct mysqli_* approach is(tested locally and the select box forms correctly) : 正确的mysqli_*方法是(在本地测试,并且选择框正确形成):

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

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 . 我没有看到您应该调用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. 您可以参考mysql_connect的用法。

Also, verify your file extension is .php 另外,请验证您的文件扩展名是.php

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);
?>

There could be few issues: 可能有几个问题:

  • Use mysqli_connect instead of mysql_connect because mysql is depreciated 使用mysqli_connect而不是mysql_connect因为mysql已mysqli_connect
  • Make 3rd parameter as empty `mysqli_connect('localhost', 'root', '');//as per standards 根据标准将第三个参数设为空的mysqli_connect('localhost','root',''); //
  • Debug your code for example: ( print_r($connect) , execute your query in phpmyadmin, print_r($result) and then in loop print_r($row) . 例如,调试代码:( print_r($connect) ,在phpmyadmin中执行查询, print_r($result)然后在循环中执行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. 最后,我相信您会得到理想的结果,并且您将详细了解正在发生的事情。

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 您可能想尝试使用此..dept_id和描述u必须根据您的数据库进行更改。希望这对您有所帮助

 <?php 
  $sql = mysql_query("SELECT dept_id ,description FROM department 
  ORDER BY dept_id ASC");
  db_select($sql,"dept_id",$dept_id,"","-select Department-","","");
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM