gpt4 book ai didi

php - 从 MYSQL 数据库检索值以显示在单选按钮中

转载 作者:行者123 更新时间:2023-11-30 00:09:42 26 4
gpt4 key购买 nike

我在从数据库检索必须在单选按钮中显示的值时遇到问题。例如,我必须选择所有“petType”并在单选按钮中显示,但它们不能重复。我有两个值“cat”和“dog”,但它只向我显示带有“dog”的单选按钮。
这是代码,它没有给我任何错误。

/* Select all categories from PetType table */
$query = "SELECT * FROM PetType ORDER BY petType";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");

/* Display text before form */
echo "<div style='margin-left: .1in'>\n
<h1 style='text-align: center'>Pet Catalog</h1>\n
<h2 style='text-align: center'>The following animal friends are waiting for you.</h2>\n
<p style='text-align: center'>Find just what you want and hurry in to the store to pick up your new friend.</p>
<h3>Which pet are you interested in?</h3>\n";

/* Create form containing selection list */
echo "<form action='ShowPets.php' method='POST'>\n";
echo "<table cellpadding='5' border='1'>";
$counter=1;
while($row = mysqli_fetch_assoc($result))
{
extract($row);
//echo "<tr><td valign='top' width='20%' style='font-weight: bold; font-size: 1.2em'\n";
echo "<input type= \"radio\" name=\"interest\" value='$petType'";
if( $counter == 1 )
{
echo "checked='checked'";
}
echo ">$petType</td>";
//echo "<td>$typeDescription</td></tr>";
$counter++;
}
echo "</table>";
echo "<p><input type='submit' value='Select Pet Type'> </form></p>\n";
?>

最佳答案

您的 html 无效。

<tr><td>在表格中添加标签,并在适当的地方添加空格。

例如

echo "<tr><td><input type= \"radio\" name=\"interest\" value='$petType' ";
if( $counter == 1 )
{
echo "checked='checked'";
}
echo ">$petType</td></tr>";

关于php - 从 MYSQL 数据库检索值以显示在单选按钮中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24185568/

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