【问题标题】:Conditional case error in Codeigniter projectCodeigniter 项目中的条件案例错误
【发布时间】:2018-11-24 13:14:41
【问题描述】:

目前,我正在开发一个股票管理应用程序。这是基于 Codeigniter 框架的。该应用程序的主要操作是管理“购买”和“问题”,就像任何这些类型的应用程序一样。

02) order_status,同一列用于记录 MySQL 表中的“purchases”和“issues”状态,名为“supplier”的列记录了相关的supplier_id 和officer_id。

03) 我在模型中使用的函数如下:

function issueDetailReport($id,$start,$end){
        $this->db->select('store_update_stock_details.item,
        CASE store_update_stock.order_status
        WHEN "purchase" THEN tbl_supplier.supplier_name
        WHEN "issue" THEN store_officer.officer_name
        END AS supplier, *');

        $this->db->from('store_update_stock');
        $this->db->join('store_update_stock_details','store_update_stock.update_stock_id=store_update_stock_details.update_stock_id');      
        $this->db->join('store_officer','store_update_stock.supplier=store_officer.officer_id ');       
        $this->db->join('tbl_supplier','store_update_stock.supplier=tbl_supplier.supplier_id ');        
        $this->db->join('store_item','store_update_stock_details.item=store_item.item_id', 'inner');
        $this->db->where("store_update_stock.status='1' and store_item.item_id=$id");
        if($start!=NULL && $end!=NULL)
        $this->db->where("store_update_stock.billed_date BETWEEN '$start' AND '$end'");     
        $this->db->order_by('store_update_stock.purchased_date','DESC');
        $q=$this->db->get();
        if($q->num_rows()>0){
            return $q->result();
        }
        return false;
    } 

04) 并查看如下:

<div class="row" style="margin-top: 2%">
        <div class="col-xs-12 table-responsive">
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>In Num</th>                   
                    <th class="text-left">Received / Issued </th>
                    <th class="text-left">Date</th>
                    <th class="text-right">Amount</th>
                    <th class="text-right">Unit Price</th>
                    <th class="text-right">Sub Total</th>

                </tr>
                </thead>
                <tbody>
                <?php
                $sub_total=0;
                $total=0;
                $no=1;
                $totalqty=0;
                if(!empty($issueDetail)){
                    foreach ($issueDetail as $rows){
                        $sub_total=(-1)*$rows->qty*$rows->unit_price;
                        $total=$total+$sub_total;
                        $qty_sub_total=(-1)*$rows->qty;
                        $totalqty=$totalqty+$qty_sub_total;
                ?>
                    <tr>
                        <td><?=$no++?></td>             
                        <td class="text-left"><?=$rows->supplier_name / officer_name?></td>                 

                        <td class="text-left"><?=$rows->purchased_date?></td>                       
                        <td class="text-right"><?=(-1)*$rows->qty?></td>                                
                        <td class="text-right"><?=$rows->unit_price?></td>
                        <td class="text-right"><?=number_format($sub_total,2)?></td>

                    </tr>
                <?php

                    }
                ?>

                    <tr>
                        <th colspan="1" class="text-left">Total </th>
                        <th></th>
                        <th></th>
                        <th class="text-right" > <?=$totalqty?></th>
                        <th></th>

                        <th class="text-right" > <?=number_format($total,2)?></th>
                    </tr>
                <?php
                }
                ?>

                </tbody>

            </table>

        </div>
        <!-- /.col -->
    </div>

05) 执行视图时,触发了以下错误。

发生数据库错误 错误号:1064

您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在 '.officer_name END AS supplier FROM store_update_stock JOIN `store_update_stoc' 在第 1 行附近使用正确的语法

选择store_update_stock_detailsitem,,CASE store_update_stockorder_status WHEN "purchase" THEN tbl_suppliersupplier_name WHEN "issue" THEN store_officerofficer_name987654332 =store_update_stock_detailsupdate_stock_idjoinstore_officeronstore_update_stock。@9876544343@@9876544444@@987654444@。 store_item ON store_update_stock_details.item=store_item.item_id 其中store_update_stock.status = '1' 和 store_item.store_item.@98767654359@ = 3361@@@986745 订购DESC

文件名:C:/xampp/htdocs/doastores/application/models/Report_model.php

行号:112

06) 但是查询在 PhPMyAdmin 中运行良好。我不明白在这一点上可能出了什么问题。谁能帮帮我?

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    我有两个想法。希望其中一个可以解决问题。

    查看查询语句的错误消息显示,我发现缺少“供应商”别名以及所有字段通配符 (*)。这通常表明查询生成器很难转义值和标识符。

    所以首先要尝试的是关闭select() 语句的“转义”,这是通过将FALSE 作为第二个参数传递来完成的。这足够安全,因为不涉及用户输入。

    要尝试的第二件事是更改select 语句的字符串,方法是使用双引号来定义purchaseissue 周围的整个引号和单引号。

    做这两件事的代码现在是这样的。

    $this->db->select("store_update_stock_details.item,
    CASE store_update_stock.order_status
      WHEN 'purchase' THEN tbl_supplier.supplier_name
      WHEN 'issue' THEN store_officer.officer_name
    END AS supplier, *", false);
    

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多