开发者

Insert statement with CodeIgniter -- so confused

开发者 https://www.devze.com 2023-04-13 09:10 出处:网络
I\'m doing well with CodeIgniter. I can do SELECT statements on my MySQL database with no problems at all. But, now I\'m trying to do an INSERT statement.

I'm doing well with CodeIgniter. I can do SELECT statements on my MySQL database with no problems at all. But, now I'm trying to do an INSERT statement.

Note that I have not tried an UPDATE statement yet.

After reading the docs, I'm so confused.

This is what I have:

contacts.php:

function add() {

    //echo "<pre>";print_r($_POST);

    $this->load->model('Contacts_model');
    $this->Contacts_model->insertContact($_POST);
}

contacts_model.php:

function insertContact($_POST) {

    //echo "<pre开发者_如何转开发>";print_r($_POST);
    $title = $_POST['title']; // I can echo this here. It works
    $f_name = $_POST['f_name']; // I can echo this here. It works



    $sql = "INSERT INTO contacts (title,f_name) " .
        "VALUES (" .
        $this->db->escape($title) .
        "," .
        $this->db->escape($f_name) .
        ")";
    $this->$db->query($sql);
}

I've read about Active Record, but if that's what is messing me up, then I still don't realize what I'm doing wrong. All of the examples look exactly like mine.

Help?

EDIT

    $sql = "INSERT INTO contacts (title,f_name) VALUES ('$this->db->escape($title)','$this->db->escape($f_name)'";
    $this->$db->query($sql);

I've also tried it like this. And many other variants. It doesn't seem to be my syntax... I think.


Your query is fine, only reason that why query is not being executed is that you are using this:

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

there is nothing like $db, just use this:

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

I'm sure this is the problem, but if it is not then please kindly post the error what it is giving. Thanks.

Hope this helps.


You missed the quote character:

$title = $this->db->escape($title);
$fname = $this->db->escape($f_name)
$sql = "INSERT INTO contacts (title,f_name) " .
    "VALUES ('{$title}', '{$fname}')";
$this->db->query($sql);

BTW, What the hell with the $_POST variable? It's one of SuperGlobal variable. You don't have to transfer it in parameter. You can always safely call it anywhere in your script.

Another note, since you use CodeIgniter, you better check out the Input class library and use it for all your input need.


Why send $_POST? Use $this->input->post("param_name") and in your instance "$this->load->model('Contacts_model');" in my practice i use "$this->load->model('Contacts_model','instance',[true or false]);" the last parameter is optional (to connect with the DB if you don't use autoload option).

Use this:

function insertContact() {
    $title = $this->input->post("title");
    $f_name = $this->input->post("f_name");
    $sql = "INSERT INTO contacts (title,f_name) " .
        "VALUES ('" . $this->db->escape($title) . "','".$this->db->escape($f_name) ."')";
    $this->$db->query($sql);
}


DON'T USE $_POST! (And use the Active Record read the user guide)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号