在动态填充的表-PHP上打印两个单独的SQL表数据

在动态填充的表-PHP上打印两个单独的SQL表数据,php,Php,有人能帮我查一下下面的代码吗?简单地说,我试图获取两个单独的SQL表的数据,一个在动态填充表的水平侧(品牌),另一个在垂直侧(分销商) 我的问题是,如果你浏览代码,我无法在数据库中显示的每个品牌名称下填充文本框。文本框仅出现在第一个品牌名称列中 我的第二个问题是如何在此处为动态填充的文本框分配唯一ID或名称 <?php $q=$_GET["q"]; include ("../connection/index.php"); $sql="SELECT * FROM distribu

有人能帮我查一下下面的代码吗?简单地说,我试图获取两个单独的SQL表的数据,一个在动态填充表的水平侧(品牌),另一个在垂直侧(分销商)

我的问题是,如果你浏览代码,我无法在数据库中显示的每个品牌名称下填充文本框。文本框仅出现在第一个品牌名称列中

我的第二个问题是如何在此处为动态填充的文本框分配唯一ID或名称

    <?php
$q=$_GET["q"];

include ("../connection/index.php"); 
$sql="SELECT * FROM distributors WHERE rsm='".$q."'";
$sqlq="SELECT * FROM brands";
$result = mysqli_query($db,$sql) or die ("SQL Error_er1");
$resultq = mysqli_query($db,$sqlq) or die ("SQL Error_er2");

echo "<table border='1'>
<tr>
<th>Distributor</th>";
"<tr>";
while($rowq = mysqli_fetch_array($resultq))
    {
    echo "<td>" . $rowq['bname'] . "</td>";

    }
"</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['dname'] . "</td>";
  echo "<td><input type='text' name='txt1'></td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($db);
?>

您创建的循环彼此之间没有关联,查询的执行也是如此

如果要解决此问题,必须将循环嵌套在一起,例如:

<?php
$q=$_GET["q"];

include ("../connection/index.php"); 
$sql="SELECT * FROM distributors WHERE rsm='".$q."'";
$result = mysqli_query($db,$sql) or die ("SQL Error_er1");

echo "<table border='1'>
<tr>
<th>Distributor</th>";
"<tr>";

//Go through all distributors
while($rowq = mysqli_fetch_array($result)) {
    echo "<td>" . $rowq['bname'] . "</td>";

    //Show all brands for current distributor
    $sqlq="SELECT * FROM brands where distributor_id = " . $rowq['rsm'];
    $resultBrands = mysqli_query($db,$sql) or die ("SQL Error Brands");

    while($row = mysqli_fetch_array($resultBrands))
    {
           $id = $row['rsm'];
        echo "<tr>";
        echo "<td>" . $row['dname'] . "</td>";
        echo "<td><input type='text' name='textBox[]'></td>";
        echo "</tr>";
    }
    //End show all brands for current distributor

}
//End Go through all distributors

<?php
$q=$_GET["q"];

include ("../connection/index.php"); 
$sql = " SELECT * FROM distributors d";
$sql .=" LEFT JOIN brands b ON (d.brand_id = b.brand_id)";
$sql .=" WHERE d.rsm=$q";
$result = mysqli_query($db,$sql) or die ("SQL Error");

echo "<table border='1'>";

while($rowq = mysqli_fetch_array($result))
{
    $id = rowq['rsm'];
    echo "<tr>";
    echo "<td>" . $rowq['dname'] . "</td>";
    echo "<td>" . $rowq['bname'] . "</td>";
    echo "<td><input type='text' name='textBox[]'></td>";
    echo "</tr>";
}


echo "</table>";

mysqli_close($db);
?>