Php从2个表中下拉选择

时间:2014-01-30 15:56:30

标签: php html sql

我创建了一个php页面,其中包含一个表格,它使用下拉列表选择过滤mysql表格数据,然后显示它们。选择组合来自2个LEFT JOINED表(供应商和供应商_费用 - supplier_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>

1 个答案:

答案 0 :(得分:0)

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

相关问题