I am new to Codeigniter and so I am having difficulty understanding how to validate my form. Whenever I hit submit or refresh the page, empty values are added to the database. Is there a way to avoid this by either asking for validation or not submitting anything if the values are empty? All the examples I find use HTML forms and if I hit NULL no on the database it brings up an error.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Controller_ca extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->load->model('Model_ca');
$result = $this->Model_ca->insert_chipper();
}
protected function form_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('name','Name' 'required');
$this->form_validation->set_rules('location', 'Location' 'required | alpha',);
$this->form_validation->set_rules('description', 'Description' 'required');
if ($this->form_validation->run() == FALSE)
{
//true
$this->load->view('Model_ca');
}
else
{
$this->index();
}
}
}
?>
<?php
class Model_ca extends CI_Model {
function __construct()
{
parent::__construct();
}
function insert_chipper() {
$name = $this->input->post('name');
$location = $this->input->post('location');
$description = $this->input->post('description');
$sql = "INSERT INTO chipper_reviews (name, location, description)
VALUES (". $this->db->escape($name).",
". $this->db->escape($location).",
". $this->db->escape($description).")";
$result = $this->db->query($sql);
}
}
?>
echo "<h1>Chip Advisor</h1><br/>";
$this->load->helper('form');
echo validation_errors();
echo form_fieldset('Add Chipper');
echo form_open('');
echo "Name:" . form_input('name');
echo "Location:" . form_input('location');
echo "Description:" . form_input('description');
echo form_submit('mysubmit', 'Submit Post!');
echo form_fieldset_close();
?>