【问题标题】:Php drop down selection from 2 tablesphp 从 2 个表中下拉选择
【发布时间】:2021-06-09 03:42:38
【问题描述】:

我创建了一个 php 页面,其中包含一个表单,该表单使用下拉选择过滤 MySQL 表数据并将它们显示在下面。该选择结合了 2 个 LEFT JOINED 表(供应商和供应商_费用 - 供应商_Id 是公共字段)中的数据并获取总费用。

我要解决的问题是如何从表单中选择特定的选择并将其显示在表格数据中。任何帮助将不胜感激。

这是我使用的代码:

<body>

    <form name="myForm" action="expenses_Supplier.php"  method="post">
    <?php
    $con=mysqli_connect("dbserver","user","****","***");
    mysqli_set_charset($con, 'utf8');
    mysqli_query($con, "SET NAMES 'utf8'");
    mysqli_query($con, "SET CHARACTER SET 'utf8'");
    mysqli_query($con, "SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    $query = "SELECT suppliers.Supplier_id,Name
    FROM suppliers
    LEFT JOIN expenses_suppliers
    ON suppliers.Supplier_id=expenses_suppliers.Supplier_id
    ORDER BY Name;"; //Write a query
    $data = mysqli_query($con, $query);  //Execute the query
    ?>
    <table  bgcolor="silver" cellpadding="4" cellspacing="4" style="width: 450px">
    <tr>
    <td style="width: 155px"><B>Name:</B></td>
    <td><select name="Supplier_id">
    <?php
    while($fetch_options = mysqli_fetch_assoc($data)) { //Loop all the options retrieved from the query
    ?>
    <option id ="<?php echo $fetch_options['Supplier_id']; ?>"  value="<?php echo $fetch_options['Supplier_id']; ?>"><?php echo $fetch_options['Name']; ?></option>
    <!--Echo out options-->
    <?php
    }
    ?>
    </select>
    </td>
    <td style="width: 155px"><input type="submit" value="ΑΝΑΖΗΤΗΣΗ"></td>
    </tr>
    </table>
    </form>
    <br />
    
    <?php
    $con=mysqli_connect("dbserver","user","****","***");
    mysqli_set_charset($con, 'utf8');
    mysqli_query($con, "SET NAMES 'utf8'");
    mysqli_query($con, "SET CHARACTER SET 'utf8'");
    mysqli_query($con, "SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    $result = mysqli_query($con,"SELECT suppliers.Name, expenses_suppliers.*
    FROM suppliers
    LEFT JOIN expenses_suppliers
    ON suppliers.Supplier_id=expenses_suppliers.Supplier_id
    where expenses_suppliers.Supplier_id=3  <== HERE IS MY PROBLEM 
    order by datepicker;");
    
    
    echo "<table border='1' width='98%' bgcolor='#f1f1f1'>";  
    echo "<tr style='font-weight: bold;'>";  
    echo "<td width='400' align='center'>ΥΠΟΛΟΙΠΟ</td>";  
    $result1 = mysqli_query($con,"SELECT SUM(DIFFERENCE) AS sup_ammount FROM expenses_suppliers where Supplier_id=3");  <== HERE IS MY PROBLEM 
    while($row=mysqli_fetch_array($result1))  
    {  
    echo "<td class='cell'>" . $row['sup_ammount'] . "</td>";
    }  
    echo "</table>";
      
    echo "<table border='1' width='98%'>
    <tr>
    <!-- <th>A/A</th> -->
    <th>Name</th>
    <th>DATE</th>
    <th>AMMOUNT</th>
    <th>CREDIT</th>
    <th>DIFFERENCE</th>
    </tr>";
    
    while($row = mysqli_fetch_array($result))
      {
      echo "<tr>";
      /* echo "<td>" . $row['Number'] . "</td>"; */
      echo "<td>" . $row['Name'] . "</td>";
      echo "<td class='cell'>" . $row['datepicker'] . "</td>";
      echo "<td class='cell'>" . $row['Ammount'] . "</td>";
      echo "<td class='cell'>" . $row['Credit'] . "</td>";
      echo "<td class='cell'>" . $row['DIFFERENCE'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysqli_close($con);
    ?> 

    </body>

【问题讨论】:

  • “选择特定的选择”到底是什么意思?是否要费用_Supplier.php 中的代码?
  • 你试过做 $variable = $_POST['Supplier_id']; ?这就是您在提交后从表单中检索所选下拉值的方式。
  • 选择表单提交的具体选择。如果下拉菜单显示 10 个结果,请从中选择一个并收集所有数据。
  • 我如何插入 $variable = $_POST['Supplier_id'];在我的查询中的 WHERE 语句中?
  • 你不能一次完成。因为您需要用户的选择才能显示正确的数据。您可以“选择”所有内容并使用一些 javascript 来显示或隐藏行,或者您可以创建另一个页面来收集正确的信息(您可以创建一个新页面或使用一些 xhr/ajax 来显示结果而无需再次加载页面) .

标签: php html sql


【解决方案1】:

您需要使用mysqli prepare 并绑定Supplier_id 值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 2020-03-04
    • 2021-10-05
    • 2020-01-23
    • 1970-01-01
    • 2015-12-14
    • 2018-12-16
    相关资源
    最近更新 更多