使用插入查询CI PHP Mysql Ajax

问题描述 投票:0回答:2

我刚接触使用Code Igniter。我想使用自定义插入查询,原因是我想在我的查询中使用uuid(),我不知道我的代码的哪一部分是错误的。我回应了查询,似乎自定义查询是正确的,我尝试直接在我的phpmyadmin上执行。请帮我。 TIA!

SSL.js

$('#btnSaveSSL').click(function(){
    var SelMerch = $('#merchantList option:selected').val();
    var SSLCert = $('#SSLCert').val();
    var SSLPath = $('#file_SSL').val();
    var ReqDate = $('#ReqDate').val();
    var ExpDate = $('#ExpDate').val();
    var Requester = $('#Requester').val();
    $.ajax({
        type:"post",
        url: baseurl + "SSLController/AddSSLRecord",
        data: {'SelMerch': SelMerch,
                'SSLCert': SSLCert,
                'SSLPath': SSLPath,
                'ReqDate': ReqDate,
                'ExpDate': ExpDate,
                'Requester': Requester},
        success:function(response) 
        {
            alert(response);
        },
        error: function(response) 
        {
            console.log(response.d);
        }
    });
});

SSLController.php

function AddSSLRecord(){
    $SelMerch = $this->input->post('SelMerch');
    $SSLCert = $this->input->post('SSLCert');
    $SSLPath = $this->input->post('SSLPath');
    $ReqDate = $this->input->post('ReqDate');
    $ExpDate = $this->input->post('ExpDate');
    $Requester = $this->input->post('Requester');
    $sql = 'INSERT INTO tbl_user(SSLID,
                                MerchantID,
                                SSL_CertName,
                                SSL_CertPath,
                                RequestDate,
                                ExpirationDate,
                                Requester)  
            VALUES (    uuid()
                     ,' .$this->db->escape($SelMerch).
                    ','.$this->db->escape($SSLCert).
                    ','.$this->db->escape($SSLPath).
                    ','.$this->db->escape($ReqDate).
                    ','.$this->db->escape($ExpDate).
                    ','.$this->db->escape($Requester).')';
     $this->SSLModel->CreateSSLRecord($sql);

    // echo "success" . $sql;
}

SSLModel.php

public function __construct() {
        parent::__construct();
        $this->load->database();
    }
function CreateSSLRecord($sql){
        $this->db->insert("tbl_ssl", $sql);  
    }
php mysql ajax codeigniter xampp
2个回答
1
投票

如果您的自定义查询是正确的并且工作正常,那么您可以考虑使用此:

$this->db->query($your_query);

但在使用此方法之前,请不要忘记清理输入。


0
投票

或者您可以使用Codeigniter提供的查询生成器:

$data = array(
 'rowname1' => 'value1',
 'rowname2' => 'value2',
 'rowname3' => 'value3' 
);

$this->db->insert('yourtablename',$data);
© www.soinside.com 2019 - 2024. All rights reserved.