简体   繁体   English

使用SQL在下拉菜单>(名称,数字)内的一行中使用SQL带有两个不同查询的下拉PHP

[英]Dropdown PHP with SQL with two different queries in one line inside dropdown > (Name, Number)

I want the dropdown to show the "client_code", "name" in one line. 我希望下拉菜单在一行中显示“ client_code”,“ name”。 It almost works but not 100%. 它几乎可以工作,但不能100%。 I am a beginner with php and SQL, can someone help me please? 我是php和SQL的初学者,有人可以帮我吗?

Code that doesn't work 代码无效

<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$queryKlantCode = "SELECT klant_code FROM klant";
$resultKlant=mysqli_query($mysqli,$queryKlant);
$resultKlantCode=mysqli_query($mysqli,$queryKlantCode);

while($row=mysqli_fetch_array($resultKlant) && 
$row2=mysqli_fetch_array($resultKlantCode)  )
{
?>

<option><?php echo $row[0]. ", ". $row2[0];?></option>
<?php
}
?>
</select>
</form>

Code that only works with retrieving name in dropdown from database 仅适用于从数据库下拉列表中检索名称的代码

<form id="thirdForm" name="form1" action="" method="post">
<select id="klantWidth">
<?php
$queryKlant = "SELECT naam FROM klant";
$res=mysqli_query($mysqli,$queryKlant);

while($row=mysqli_fetch_array($res))
{
?>
<option><?php echo $row[0]; ?></option>
<?php
}
?>
</select>
</form>

You can select more than one column from a table in the same select, and as both these columns live in the same table it makes producing this result much simpler. 您可以从同一选择表中选择一个以上的列,并且由于这两个列都位于同一表中,因此使生成此结果变得更加简单。

<form id="thirdForm" name="form1" action="" method="post">
    <select id="klantWidth">

<?php
    $sql = "SELECT naam, klant_code FROM klant";

    $result = mysqli_query($mysqli,$sql);

    while($row=mysqli_fetch_array($result)){
?>

        <option><?php echo $row[0]. ", ". $row[1];?></option>
<?php
    }
?>  
    </select>
</form>

You probably want to do this with your <option> tag as well rather than put the name and code in the visible portion 您可能还希望使用<option>标记执行此操作,而不是将名称和代码放在可见的部分中

    <option value="<?php echo $row[1];?>"><?php echo $row[0];?> </option>

And if you use mysqli_fetch_assoc() you can use the columns names so you know what you are putting where 而且,如果您使用mysqli_fetch_assoc() ,则可以使用列名称,以便知道要放在哪里

while($row=mysqli_fetch_assoc($result){

    <option value="<?php echo $row['klant_code'];?>"><?php echo $row['naam'];?> </option>

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

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