简体   繁体   English

使用PHP从MSSQL数据填充选择框

[英]populate select box from MSSQL data with PHP

I've searched and found some answers that are close, but not quite what I need. 我已经搜索并找到了一些接近的答案,但并不是我所需要的。 I'm hoping someone here can point me in the right direction. 我希望这里的人可以指出正确的方向。

I have a work assignment that I don't fully know how to do. 我的工作任务不完全知道该怎么做。 I have a basic knowledge of PHP and SQL, but a solid understanding of HTML and CSS. 我具有PHP和SQL的基本知识,但是对HTML和CSS有扎实的理解。

SO, I built a beautiful web form with lots of select boxes. 因此,我建立了一个带有许多选择框的漂亮Web表单。 Some will pull data right from a MS SQL database. 有些会直接从MS SQL数据库中提取数据。 When submitted, some will push data back into the database. 提交后,有些会将数据推回到数据库中。 For this question I wan to focus on pulling from the DB. 对于这个问题,我希望集中精力从数据库中获取。

I did get the PHP page connected to the database, but getting the select boxes to populate is giving me a real hard time. 我确实将PHP页面连接到数据库,但是要填充选择框却给了我很大的麻烦。 I want to learn how to do this. 我想学习如何做。

My form page is calling a config.php file with the connection info like this: 我的表单页面正在使用以下连接信息调用config.php文件:

<?php
//Require config file with DB connection details
require_once('config.php');
?>

My config file looks like this: 我的配置文件如下所示:

<?php
$serverName = "xxxxxxxxx\xxxxxx"; //serverName\instanceName
$connectionInfo = array( "Database"=>"xxxxx", "UID"=>"xxxxx", "PWD"=>"xxxxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
 echo "Congratulations! Your DB connection has been established.<br />";
}else{
 echo "Connection could not be established.<br />";
 die( print_r( sqlsrv_errors(), true));
}
?>

My page displays the message that the connection is successful. 我的页面显示连接成功的消息。 My big question is this, what else to I have to put on my form page to get it to display data in a select box? 我的大问题是,还需要在表单页面上添加哪些内容才能在选择框中显示数据?

Here what i was trying but isnt working... 在这里我正在尝试但没有工作...

<?php
//Require config file with DB connection details
require_once('config.php');

$result = mssqli_query($con,"SELECT * FROM Auditors ORDER BY ID");

while($row = mssqli_fetch_array($result))
  {
  echo $row['AuditorName'];
  echo "<br>";
  }

mysqli_close($con);
?>

And then the select box code: 然后选择框代码:

<select name="auditor" id="auditor">
<?php foreach ($result as $row): ?>
    <option><?=$row["Auditor"]?></option>
    <?php endforeach ?>
</select>

Can anyone tell me what I'm doing wrong and maybe post an example of what WILL work? 谁能告诉我我做错了什么,也许可以举个例子说明我会做些什么? Once I get this part figured out, I have several other select boxes to apply it to, so this will be a great help. 一旦弄清楚了这一部分,我便可以将其应用于其他几个选择框,因此这将是很大的帮助。

The above code gets me a blank page with only my successful DB connection message. 上面的代码使我只有一个成功的数据库连接消息,空白页。 My whole form disappears and there is no error message. 我的整个表单消失了,没有错误消息。

Thanks in advance for your time, Jason 预先感谢您的时间,杰森

Instead of using both while and foreach you can make it work by bringing your while loop down 不必同时使用while和foreach,而可以通过关闭while循环来使其工作

<select>
<?php
while($row = mssqli_fetch_array($result))
{  
   echo '<option>' . $row["Auditor"] . '</option>';
}
?>
</select>

<?php
   mysqli_close($con);
?>

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

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