如何将数组作为参数传递给mysql中的存储过程?

时间:2018-01-04 09:44:16

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

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

我试图将按钮点击视图中的电子邮件和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 ;

欢迎任何形式的帮助,提前谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 我无法在$certID中的任何位置看到sendMail()。如果你打印出来,你会得到什么价值?
  2. 我很确定您只使用$this->input->post('sendMail');代替$this->input->post('sendMail[]');来获取数组
  3. 您确认您的(不安全)SQL调用是否适用于自定义值?如果是,请确保传递正确的值
  4. 尝试调试和排除故障,您可能会发现原因。