gpt4 book ai didi

PHP - 下拉列表填充但不会使用 2 个表进行选择

转载 作者:行者123 更新时间:2023-11-30 00:34:10 24 4
gpt4 key购买 nike

我已经为此工作了至少一个月,并且在其他地方找不到这个问题。我知道我只是错过了一些愚蠢的东西,但是......

有 2 个表:biz 和 bizclass。 bizclass.bizclassName 包含 250 多个分类来填充下拉框。从 bizclass 表填充和更新到 biz 表都有效,但是当我尝试从 biz.bizClass1 选择当前数据时,下拉列表初始值设置为空值“Select Class”。请帮忙。这让我发疯。我已经老了,不能再经历更多不眠之夜了!新手 - 对 php 和 javascript 有点熟悉,但还没有掌握 ajax。

echo "Class1: <select name ='bizClass1' id='bizClass1'/> ";

$sql = 'SELECT bizclassName FROM bizclass ORDER BY bizclassName';
$query2 = 'SELECT `bizClass1` FROM `biz` WHERE `bizID` = "'. $search .'"';
$clist = mysqli_query($connection,$sql);
$num=mysqli_num_rows($clist);
$olist = mysqli_query($connection, $query2); // select bizClass from biz

$bizTblRecord = mysqli_fetch_assoc($clist); // option values from bizclass table to populate the dropdown
$row2 = mysqli_fetch_assoc($olist); // fetched the bizClass from biz

if ($row2['bizClass1'] == $bizTblRecord['bizclassName']){
printf ("<option value='%s' selected >%s</option> ", $row2['bizClass1'], $row2['bizClass1']);
} else {
printf ("<option value=''>Select Class</option> ");
}//end if


for($numrows=1; $numrows<= $num; $numrows++)
{
// Associative array
$row=mysqli_fetch_assoc($clist);

printf ("<option value='%s'>%s</option>",$row['bizclassName'],$row['bizclassName']);

} //end for

echo "</select>";

// Free result set
mysqli_free_result($clist);
mysqli_free_result($olist);

最佳答案

    echo "Class1: <select name ='bizClass1' id='bizClass1'/>
<option value=''>Select Class</option> ";

$sql = 'SELECT bizclassName FROM bizclass ORDER BY bizclassName';
$clist = mysqli_query($connection,$sql);
$num=mysqli_num_rows($clist);



$querySEARCH = 'SELECT `bizClass1` FROM `biz` WHERE `bizID` = "'. $search .'"';
$SEARCHlist = mysqli_query($connection, $querySEARCH); // select bizClass from biz
$rowSEARCH= mysqli_fetch_assoc($SEARCHlist); // fetched the bizClass from biz

for($numrows=1; $numrows<= $num; $numrows++)
{
// Associative array
$row=mysqli_fetch_assoc($clist);

if ($rowSEARCH['bizClass1'] == $row['bizclassName']){
printf ("<option value='%s' selected >%s</option> ", $rowSEARCH['bizClass1'], $rowSEARCH['bizClass1']);
} else {
printf ("<option value='%s'>%s</option>",$row['bizclassName'],$row['bizclassName']);
}//end if

} //end for

echo "</select>";

// Free result set
mysqli_free_result($clist);
mysqli_free_result($SEARCHlist);

If 语句必须位于循环内才能将每个选项与搜索进行比较。

测试表:

业务

bizID   bizClass1
1 ronaldo
2 shevshenko
3 falcao
4 zidane
5 valderrama

商业类

bizclassName
falcao
ozil
ronaldo
messi
shevshenko
valderrama
hazard
totti

关于PHP - 下拉列表填充但不会使用 2 个表进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22305897/

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