【问题标题】:How to pass array as parameter to stored procedure in mysql?如何将数组作为参数传递给mysql中的存储过程?
【发布时间】:2018-06-14 00:31:25
【问题描述】:

我正在尝试将数组作为参数传递给我的存储过程。但我没有得到预期的结果。

我正在尝试通过单击按钮将视图中的电子邮件和 ID 列表发送到控制器,然后我试图通过将该列表作为参数传递给我的存储过程来从数据库中获取数据。

这是我的代码:

查看:

<form action="<?= base_url('certificate/sendMail') ?>" method="post">
    <table id="example1" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Mail</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th style="text-align: center;">
                    <input type="submit" name="submit" value="Send" class="send btn btn-primary btn-xs" disabled="" />
                </th>
            </tr>
        </tfoot>
        <tbody>
            <tr>
                <td style="text-align: center;">
                    <input type="checkbox" class="checkbox" name="sendMail[]" value="<?= $certify->landlord_main_email; ?>">
                </td>   
            </tr>
        </tbody>
    </table>
</form>

在这里,我通过选中复选框来获取电子邮件和 ID 的列表。

控制器:

public function sendMail($certID)
    {
        # code...

        if(isset($_POST['submit'])){//to run PHP script on submit
            if(!empty($_POST['sendMail'])){
            // Loop to store and display values of individual checked checkbox.
                foreach($_POST['sendMail'] as $selected){
                    //echo $selected."</br>";
                }
            }
        }
        $result['certMails'] = $this->c->getCertificateByEmail($certID);
        $email_to = $this->input->post('sendMail[]');
        $body = $this->load->view('Emails/emailAll', '', true);
    }

我在这里尝试获取数据并将其发送到电子邮件模板视图。

型号:

public function getCertificateByEmail($certID)
    {
        # code...
        $query = $this->db->query("call getCertificateByEmail($certID)");

        if ($query) {
            $data = $query->result();
            $query->next_result(); 
            $query->free_result();
            return $data;
        } else {
            return false;
        }
    }

存储过程:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Certificate_By_Email`(IN `certID` INT)
BEGIN
SELECT `cert_id`, `cert_user_id`, `cert_landlord_id`,
        tbl_landlord.landlord_id, tbl_landlord.landlord_main_email, 
        `cert_property_id`, tbl_property.property_code, 
        tbl_property.property_city,  tbl_property.property_cluster, 
        tbl_cluster.cluster_name, `cert_status`, `cert_number`, 
        tbl_certificates.certificate_name, `cert_issue_date`, 
        `cert_expiry_date`, `cert_duration`, `cert_updated_date`, 
        `cert_remarks`, `cert_notes` 
FROM `tbl_certificate_details` 
    LEFT OUTER JOIN tbl_landlord ON  tbl_certificate_details.cert_landlord_id = tbl_landlord.landlord_id 
    LEFT OUTER JOIN tbl_property ON tbl_certificate_details.cert_property_id = tbl_property.property_id 
    LEFT OUTER JOIN tbl_certificates ON tbl_certificate_details.certificate_id = tbl_certificates.certificate_id 
    LEFT OUTER JOIN tbl_cluster ON tbl_property.property_cluster = tbl_cluster.cluster_id 
    LEFT OUTER JOIN tbl_area ON tbl_property.property_area = tbl_area.area_name 
WHERE FIND_IN_SET(cert_id, certID);
END$$
DELIMITER ;

欢迎任何形式的帮助,在此先感谢。

【问题讨论】:

  • 这个问题已经被问过很多次了

标签: php mysql stored-procedures parameters codeigniter-3


【解决方案1】:
  1. 我在sendMail() 的任何地方都看不到$certID 的定义。如果你把它打印出来,你会得到什么价值?
  2. 我很确定您只是使用 $this-&gt;input-&gt;post('sendMail'); 而不是 $this-&gt;input-&gt;post('sendMail[]'); 来获取数组
  3. 您是否确认您的(不安全的)sql 调用适用于自定义值?如果是这样,请确保您传递了正确的值

尝试调试和故障排除,您可能会找到原因。

【讨论】:

  • $certID 正在打印证书 ID,我将其传递给模型。如果我只是使用 $this->input->post('sendMail');这将给我一个邮件 ID 而不是数组。如果我从 phpmyadmin 执行它,我的存储过程会给我结果。
  • 如果您将$certID 替换为您知道可以使用的字符串,那么会发生什么?
猜你喜欢
  • 2012-10-13
  • 2018-04-04
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 2010-10-19
  • 1970-01-01
相关资源
最近更新 更多