如何在CodeIgniter中正确地将表单数据传递给数据库函数

时间:2014-03-10 10:11:48

标签: php codeigniter

我是CI的新手,一直在阅读文档并关注form tutorial。但是我很难将我的表单写入我的数据库。实际上,数据正在写入,但我收到错误......

A PHP Error was encountered

Severity: Notice

Message: Use of undefined constant firstname - assumed 'firstname'

Filename: controllers/form.php

Line Number: 25

我也为第二个字段写错了我正在写姓氏。我认为我已经足够接近MySQL正在弄清楚我想要做什么,但我仍然缺少一些东西。

我的表格/视图......

<html>
<head>
<title>Pledge Details</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('form'); ?>

<h5>First Name</h5>
<input type="text" name="firstname" value="<?php echo set_value('firstname'); ?>" size="50" />

<h5>Last Name</h5>
<input type="text" name="lastname" value="<?php echo set_value('lastname'); ?>" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />

<h5>Kms/Miles</h5>
<select name="myselect">
<option value="Kms" <?php echo set_select('myselect', 'Kms', TRUE); ?> >Kms</option>
<option value="Miles" <?php echo set_select('myselect', 'Miles'); ?> >Miles</option>
</select> 

<h5>Pledge</h5>
<input type="text" name="pledge" value="<?php echo set_value('pledge'); ?>" size="50" />


<br><br>

<div><input type="submit" value="Submit" /></div>

</form>

<?php echo form_close() ?>

</body>
</html>

我的控制器......

<?php

class Form extends CI_Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('firstname', 'First Name', 'trim|required|max_length[12]|xss_clean');
        $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|max_length[12]|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('pledge', 'Pledge', 'trim|required|max_length[12]|decimal|xss_clean');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->database();

            $sql = "INSERT INTO donations (firstname, lastname)
                VALUES (".$this->db->escape($_POST[firstname]).", ".$this->db->escape($_POST[lastname]).")";

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

            echo $this->db->affected_rows(); 

            $this->load->view('formsuccess');
        }
    }
}
?>

2 个答案:

答案 0 :(得分:2)

您的错误来自以下一行

$sql = "INSERT INTO donations (firstname, lastname) VALUES (".$this->db->escape($_POST[firstname]).", ".$this->db->escape($_POST[lastname]).")";

您忘记将$_POST个变量括在引号中。请注意$_POST[firstname]成为$_POST['firstname']的方式。它应该是

$sql = "INSERT INTO donations (firstname, lastname) VALUES (".$this->db->escape($_POST['firstname']).", ".$this->db->escape($_POST['lastname']).")";

也像其他人说的那样,而不是$_POST['firstname'],你可以做$this->input->post('firstname');

答案 1 :(得分:0)

试试这个:

$firstname = $this->input->post('firstname');
$lastname = $this->input->post('lastname');

$this->load->database();

$this->db->insert('mytable', array('firstname'=>$firstname , 'lastname'=>$lastname));


echo $this->db->affected_rows(); 

$this->load->view('formsuccess');

最好使用活动记录,并尝试将mysql进程放在模型中,而不是放在控制器中,因为codeigniter是为该MVC设计的