2

I am new to CI and have been reading documentation and following a form tutorial. However I'm struggling to get my form written to my database. Actually, the data is being written, but I'm getting the error...

A PHP Error was encountered

Severity: Notice

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

Filename: controllers/form.php

Line Number: 25

I also ge the error for the second field I'm writing lastname. I think I'm close enough that MySQL is figuring out what I'm trying to do, but I'm still missing something.

My Form/View...

<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>

My controller...

<?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');
        }
    }
}
?>
1
  • 2
    why are you using $_POST when you can use $this->input->post('firstname'); Commented Mar 10, 2014 at 10:16

2 Answers 2

2

Your error is coming from the following line

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

You forgot to enclose your $_POST variables in quotes. Notice how $_POST[firstname] has become $_POST['firstname']. It should be

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

Also like others have said rather than doing $_POST['firstname'] you can do $this->input->post('firstname');

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks:) What is the difference?
0

Try this:

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

It's better to use active record and also try putting the mysql process in your model and not in the controller because codeigniter is designed for that MVC

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.