使用表单字段的值作为参数[php / cakePhp]执行SQL查询

时间:2018-07-06 14:44:07

标签: php cakephp

我正在用Cakephp 3做一个应用程序。我的问题是我想执行一个SQL查询,该查询的参数是表单字段的值。我搜索并尝试过,但我不能。 我尝试使用以下方法检索该字段的值:onChange()在另一个输入字段中的Javascript,但是我无法使用该变量来执行查询sql。 如果您有其他解决方法或解决方案,我在听。 目的是检索查询的值,以便与表单的其他字段进行计算。

查询的参数为:'centre%'

    <?php
    /**
     * @var \App\View\AppView $this
     * @var \App\Model\Entity\CountersSite $countersSite
     */
    ?>
    <nav class="large-3 medium-4 columns" id="actions-sidebar">
        <ul class="side-nav">
            <li class="heading"><?= __('Actions') ?></li>
            <li><?= $this->Html->link(__('List Counters Sites'), ['action' => 'index']) ?></li>
            <li><?= $this->Html->link(__('List Sites'), ['controller' => 'Sites', 'action' => 'index']) ?></li>
            <li><?= $this->Html->link(__('New Site'), ['controller' => 'Sites', 'action' => 'add']) ?></li>
            <li><?= $this->Html->link(__('List Counters'), ['controller' => 'Counters', 'action' => 'index']) ?></li>
            <li><?= $this->Html->link(__('New Counter'), ['controller' => 'Counters', 'action' => 'add']) ?></li>
        </ul>
    </nav>
    <div class="countersSites form large-9 medium-8 columns content">
        <?= $this->Form->create($countersSite) ?>
        <fieldset>
            <legend><?= __('Add Counters Site') ?></legend>
            <?php
                echo $this->Form->control('site_id', ['options' => $sites]);
                echo $this->Form->control('counter_id', ['options' => $counters]);
                echo $this->Form->control('date');
                echo $this->Form->control('index_gas_m3');

                 // test request sql with parameter
                $link = mysqli_connect("localhost", "root", "", "db_webapp");
                $query = "SELECT  SUM(value) as value
                        FROM
                                ( 
                                    select (counters_sites.cumulative_degree) as value
                                            from counters_sites, sites
                                            where sites.site_id = counters_sites.site_id
                                            and MONTH(counters_sites.date) = MONTH(NOW())-1
                                            and lower(sites.name) LIKE 'centre%'

                                    UNION ALL

                                    select (degrees.real_value) as value
                                            from degrees, sites
                                            where sites.site_id = degrees.site_id
                                            and MONTH(degrees.date) = MONTH(NOW())
                                            and lower(sites.name) LIKE 'CENTRE%'

                                )s ";
                $result = mysqli_query($link, $query);
                while($row = mysqli_fetch_array($result))  
                {  
                     echo $row["value"];   
                } 

                //end request with prameter 
                
                echo $this->Form->control('index_gas_mwh');
                echo $this->Form->control('ecs_index_m3');
                echo $this->Form->control('ecs_index_mwh');
                echo $this->Form->control('full_index');
                echo $this->Form->control('thermal_coefficient');
                echo $this->Form->control('corrector_coefficient');
                echo $this->Form->control('cumulative_degree');
                echo $this->Form->control('gas_consumption_m3');
                echo $this->Form->control('gas_consumption_mwh');
                echo $this->Form->control('cumulative_gas_consumption');
                echo $this->Form->control('ecs_consumption_m3');
                echo $this->Form->control('ecs_consumption_mwh');
                echo $this->Form->control('cumulative_ecs_consumption');
                echo $this->Form->control('nc_consumption');
                echo $this->Form->control('nb_consumption');
                echo $this->Form->control('gap_value');

                

            ?>
        </fieldset>
        <?= $this->Form->button(__('Submit')) ?>
        <?= $this->Form->end() ?>
    </div>

0 个答案:

没有答案