gpt4 book ai didi

PHP/MySQL 下拉菜单

转载 作者:行者123 更新时间:2023-11-30 23:49:12 24 4
gpt4 key购买 nike

<分区>

我刚刚开始学习 PHP/MySQL,最近几天一直在尝试获取一个下拉菜单来填充我的数据库中的选项。我已经阅读了几个教程,并尝试应用它们,但我无法正常工作。

我想做什么:我需要一个名为“基因型”的下拉菜单,它从我的数据库中名为“小鼠”的表的“基因型”列中提取基因型列表。

问题:当我点击提交按钮时,它会连接到我的数据库并执行查询。但是我无法从下拉菜单中显示任何选项。

到目前为止,这是我的代码(如果我应该发布 mousecount.php,请告诉我)

<?php

include('connection.txt');

$conn = mysqli_connect($server, $user, $pass, $dbname, $port)
or die('Error connecting to MySQL server.');
?>

<html>
<head><title>Crap That Won't Work</title></head>
<body bgcolor="white">

<hr>
<dt>THINGS ARE TOTALLY NOT WORKING</dt>
<dd>
<form action="mousecount.php" method="POST">
<table><tr>
<td><p><label for="Genotype">Genotype:</label><br/>
<select name="Genotype">
<?php
$query = "SELECT MouseID, Genotype FROM Mice";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result))
{
print "<option value=\"$row['Genotype']\">".$row['Genotype']."</option>";
}
?>
</select>
</p></td>
</tr></table><br/>
<input type="submit" value="Submit">
</form>
</body>
</html>


编辑(已解决):

我无法让下拉菜单填充 MySQL 数据库中的选项。感谢您的帮助。我正在发布我的最终代码,以防它对其他人有所帮助。

<!-- Connect to database -->
<?php
include('connection.php');
$conn = mysqli_connect($server, $user, $pass, $dbname, $port)
or die (mysql_error());
?>

<hr>

<h4>Mice Count</h4>
<form action="mousecount.php" method="POST">
<p><label for="Genotype">By Genotype:</label><br/>
<?php
$query = "SELECT Genotype from Mice GROUP BY Genotype";
$result = mysqli_query($conn, $query);
echo "<select name='Genotype'>";
echo "<option value='0'>-Select-</option>";
while($row = mysqli_fetch_array($result)) {
echo "<option value='".$row['Genotype']."'>".$row['Genotype']."</option>";
}
echo "</select>";
?>
</p>
<input type="submit" value="Submit">

</form>

</body>
</html>

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com