php - How to correctly pass form data to database function in CodeIgniter -


i new ci , have been reading documentation , following form tutorial. i'm struggling form written database. actually, data being written, i'm getting error...

a php error encountered  severity: notice  message: use of undefined constant firstname - assumed 'firstname'  filename: controllers/form.php  line number: 25 

i ge error second field i'm writing lastname. think i'm close enough mysql figuring out i'm trying do, 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 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');         }     } } ?> 

your error coming following line

$sql = "insert donations (firstname, lastname) values (".$this->db->escape($_post[firstname]).", ".$this->db->escape($_post[lastname]).")"; 

you forgot enclose $_post variables in quotes. notice how $_post[firstname] has become $_post['firstname']. should be

$sql = "insert donations (firstname, lastname) values (".$this->db->escape($_post['firstname']).", ".$this->db->escape($_post['lastname']).")"; 

also others have said rather doing $_post['firstname'] can $this->input->post('firstname');


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -