用 CodeIgniter 插入语句——太困惑了

发布于 2024-12-10 04:32:38 字数 1206 浏览 0 评论 0原文

我用 CodeIgniter 做得很好。我可以毫无问题地在我的 MySQL 数据库上执行 SELECT 语句。但是,现在我正在尝试执行 INSERT 语句。

请注意,我还没有尝试过 UPDATE 语句。

读完文档后,我很困惑。

这就是我所拥有的:

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);
}

我读过有关Active Record的内容,但如果这就是让我困惑的原因,那么我仍然没有意识到我做错了什么。所有的例子看起来都和我的一模一样。

帮助?

编辑

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

我也尝试过这样的。还有许多其他变体。这似乎不是我的语法...我想。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情癖 2024-12-17 04:32:38

您的查询很好,查询没有被执行的唯一原因是您正在使用这个:

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

没有像 $db 这样的东西,只需使用这个:

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

我确信这是问题所在,但如果不是,请发帖它给出的错误。谢谢。

希望这有帮助。

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.

迟月 2024-12-17 04:32:38

您错过了引号字符:

$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);

顺便说一句,$_POST 变量到底是怎么回事?它是超级全局变量之一。您不必在参数中传输它。您始终可以在脚本中的任何位置安全地调用它。

另请注意,由于您使用 CodeIgniter,因此最好查看 Input 类库 并用它来满足您的所有输入需求。

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.

薄情伤 2024-12-17 04:32:38
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)
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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文