Codeigniter model does not insert value

This is my second web application in CI.

I am trying to get a single value from a form (after submitting) and paste it into the database.

I have a urlsubmission controller:

<?php class Urlsubmission extends CI_Controller{ function index() { $this->load->model('domaincheckmodel'); $this->domaincheckmodel->verifyduplicates(); } } ?> 

model (domaincheckmodel) - which looks for db for any duplicate:

 <?php class Domaincheckmodel extends CI_Model{ function __construct(){ // Call the Model constructor parent::__construct(); } function verifyduplicates(){ #PREPARE DATA $sql = "SELECT tld from ClientDomain WHERE tld = ?"; $postedTLD = $_POST['urlInput']; // Get unsanitized data $endquery = $this->db->query($sql,array($this->db->escape_str($postedTLD))); // Query db #CONDITION if($endquery->num_rows() > 0){ $this->load->view('err/domainexists'); ##domain already used ## please login.. } else{ #number of rows must be 0, insert into db $newDomain = "INSERT INTO ClientDomain(tld) VALUES('".$this->db->escape_str($postedTLD)."')"; $this->load->view('success/domainfree'); ## please register } } } ?> 

and two silly looks right now: domainexists and domainfree:

domainexists:

 <h2>Domain Exists</h2> 

domainfree:

 <h2>Domain free</h2> 

The problem is this: when I run the script, there are no errors, but it correctly executes the else{} block, but does not enter anything into the database.

I have a config / autoload setting to automatically load db lib, for example:

 $autoload['libraries'] = array('database'); 

What am I doing wrong here?

+4
source share
2 answers

You are not using an SQL query. Do it.

 $newDomain = "INSERT INTO ClientDomain(tld) VALUES('".$this->db->escape_str($postedTLD)."')"; $this->db->query($newDomain); 

or you can use simple_query ()

 $newDomain = "INSERT INTO ClientDomain(tld) VALUES('".$this->db->escape_str($postedTLD)."')"; $this->db->simple_query($newDomain); 
+1
source

Do you declare an INSERT statement but never execute it?

0
source

All Articles