【问题标题】:Pulling data via LEFT JOIN with multiple data per ID通过 LEFT JOIN 提取数据,每个 ID 有多个数据
【发布时间】:2021-05-19 00:49:53
【问题描述】:

我有一个问题,我将我的客户 ID 表左连接到我的免税表,当我在同一客户下有多个免税时,它只是提取与该客户关联的数据的第一个系统 ID,或者根本没有数据。我有 3 个提取相同的豁免,1 个除了客户姓名之外根本没有提取任何数据,其余的似乎还可以

Exemption Table

Tax Exemption MySQL table

Customer List mySQL table

这是列出所有要点击的客户的表单页面。对于没有特定用户 ID 的任何人,我都禁用了要单击的链接。

<table>

    <tr>
        <th>System ID</th>
        <th>Customer</th>
        <th>Fuel Type</th>
        <th>Tax Type</th>
        <th>Category</th>
        <th>Expiration Date</th>
       
    </tr>

<?php
$IDUser = $_SESSION['userid']; 

   $sql = "SELECT * FROM TaxExemption LEFT JOIN CustomerList ON TaxExemption.CustomerID = CustomerList.ID ORDER BY Customer, FuelType ";

      $result = $conn->query($sql);


          if ($result->num_rows > 0) {

      while($row = $result->fetch_assoc()) {

        echo "<tr><td>".$row['ID']."</td>";
        
        
        
        $customerid = $row['CustomerID']; 
       
       
               $sql = "SELECT * FROM customerlist WHERE ID = '$customerid' ";
       
             $result1 = $conn->query($sql);
       
       
                 if ($result1->num_rows > 0) {
       
             while($row1 = $result1->fetch_assoc()) {
              
if($IDUser == '1' || $IDUser == '2' || $IDUser == '151' || $IDUser == '270') {echo "<td><a href='financefueltaxedit.php?ID=".$row['ID']."'>".$row1['Customer']."</a></td>"; }
              else
                {echo "<td>".$row1['Customer']."</td>";}
              
                 }}
       
        
       
  echo "     
        <td>".$row['FuelType']."</td>
        <td>".$row['TaxType']."</td>
        <td>".$row['Category']."</td>
        <td>".date_format(date_create($row['Expiration']),'m/d/Y')."</td></tr>
        
        
        ";

      } }



else{echo "<tr><td style='text-align: center' colspan='5'>Currently No Exemptions Added</td></tr>";}

?>

</table>

</div>

这是提取信息的编辑页面 - 如果客户有多个豁免,则存在问题,因为他们都有不同的系统 ID,但客户编号相同

<div class="container">


    <h2>Customer Exemption Detail</h2>


    <ul>
      <li style='background: #324b81; color:white' id='profile'>Profile</li>
    </ul>

   <br><br>
   <p>Customer: <?php echo $row['Customer']?></p>
 

   

    <form class='updatecustomer'>

    <input name="ID" value="<?php echo $ID; ?>" hidden="hidden">

    <table>


        <tr>
                <th>Customer ID:</th>
                <td><input type="text" name='CustomerID' value="<?php echo $row['CustomerID']?>" readonly disabled></td>
        </tr>

        <tr>
                <th>Name:</th>
                <td><input type="text" name='name' value="<?php echo $row['Customer']?>" readonly disabled></td>
        </tr>
        
        <tr>
                <th>FuelType:</th>
                <td><input type="text" name='FuelType' value="<?php echo $row['FuelType']?>" readonly disabled></td>
        </tr>
        
         <tr>
                <th>TaxType:</th>
                <td><input type="text" name='TaxType' value="<?php echo $row['TaxType']?>" readonly disabled></td>
        </tr>
        
        <tr>
                <th>Expiration:</th>
                <td><input type="text" name='Expiration' value="<?php echo $row['Expiration']?>"></td>
        </tr>
                
     
    
    </table>
    
    <br>

<input type="button" value="Update" id="update">
   
<input type="reset" value="Cancel" onclick="location.href='financefueltax.php';">


    </form>
    
<?php           }} ?>


</div>



<script type="text/javascript">

    

    $(document).ready(function(){

        $('#update').click(function(event){

            event.preventDefault();

        var data = $('.updatecustomer').serialize();

            $.ajax ({             

                data:data,
                
                url:'processor-financefueltaxedit.php',
                
                type:'post'

            });

        
    function redirect(){  

        location.href='financefueltax.php';

      }

      

      setTimeout(redirect,500);



    });

    });
  

  $(document).ready(function(){


        var d = new Date();
        var day = d.getDate();
        var month = d.getMonth();
        var year = d.getFullYear(); 


    $('[name="statedate"], [name="feddate"], [name="Expiration"] ').datepicker({

        

        dateFormat:'yy-mm-dd',
        minDate: new Date(year - 1, month, day - 7),
        maxDate: new Date(year + 2, month, day)

        
      });


});

</script>
</body>

这是我的处理器页面 - 允许单一豁免客户正确显示 - 我只希望能够更改日期并将新的到期日期发布到 taxexemptions 表,我不希望编辑其他信息因此只读字段- 这就是为什么我只有一个日期的更新声明。

<?php

require 'wconnectionfile.php';

$date = $_POST['date'];
$date = date_create($date);
$date = date_format($date, "Y-m-d");

$CustomerID = $_POST['CustomerID'];
$fueltype = $_POST['fueltype'];
$taxtype = $_POST['taxtype'];
$statetax = $_POST['statetax'];
$fedtax = $_POST['fedtax'];

if (!empty($date)) {
 
 if (!empty($fedtax)) {$taxtype = $fedtax; }  else {$taxtype = $statetax;}
 
$sql = "UPDATE taxexemption LEFT JOIN CustomerList ON TaxExemption.CustomerID = CustomerList.ID SET taxexemption.Expiration='$date' WHERE taxexemption.CustomerID='$CustomerID'";
 

if ($conn->query($sql) === TRUE) {

    

}

 

} else {

    echo "Error: " . $sql . "<br>" . $conn->error;

}



$conn->close();


?>

对于多重豁免我不知道该怎么做,更不用说它没有将更新的日期从日期选择器发布到数据库

【问题讨论】:

    标签: javascript php html jquery mysql


    【解决方案1】:

    对于多重豁免问题,由您或您的客户决定如何处理;你可以:

    • 允许每个客户多次免税,并一次为与客户相关的免税创建一个编辑页面
    • 不允许多次免税,为此您必须删除每个客户重复的免税

    【讨论】:

    • 不会选项 A 让我在同一条船上,其中 id 仍然必须弄清楚如何将不同的关联系统 ID 拉到单个客户端页面中,如果我可以在那里做到,我可以做到它在列表视图的豁免?我喜欢将客户下的所有豁免列为单个页面的想法,但他们希望轻松查看哪些即将过期,这就是为什么他们的巨大列表视图。
    • 对于选项 A,客户页面将包含他所有免税的表格(即系统 ID)
    • 我最终将 taxexemptions 的主键字段更改为 ExemptionID,因为我的问题是让行结果显示 $row['ID'];并且两个表都有一个名为 ID 的字段,因此它会提取 customerlist ID 而不是 taxexemption ID;通过将免税 ID 更改为豁免 ID,我可以使 onclick 成为“exemptionID”而不是“ID”,后者通过 JOIN 不断拉客户列表。 - 仍然搞砸这个,但我让它正确显示......现在尝试发布数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多