user2227553
user2227553

Reputation:

Codeigniter How to save data from a form to a database

Codeigniter

Hi. I want to send my form data to a database. The data are checked ok but what to do next?

My controlls

 <?php

class Form extends CI_Controller {

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

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

            $this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha'); 
            $this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha'); 
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
            $this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]'); 
            $this->form_validation->set_rules('brand', 'Marka', 'required|alpha'); 
            $this->form_validation->set_rules('model', 'Model', 'required|alpha'); 
            $this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]'); 
            $this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]'); 
            $this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]'); 
            $this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]'); 
            $this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]'); 

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

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

    }


}
?>

My view

    <html>
<head>
<title>My Form</title>
</head>
<body>


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

<h5>Wpisz swoje imię</h5>
<?php echo form_error('name'); ?>
<input type="text" name="name" value="<?php echo set_value('name'); ?>" size="50" />

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

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

<h5>Numer telefonu</h5>
<?php echo form_error('number'); ?>
<input type="text" name="number" value="<?php echo set_value('number'); ?>" size="50" />

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

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

<h5>Rok produkcji</h5>
<?php echo form_error('year'); ?>
<input type="text" name="year" value="<?php echo set_value('year'); ?>" size="50" />


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



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

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

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



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

</form>

</body>
</html>

I don't expect the final solution, but I need help what is the next step

My datebase:

Upvotes: 0

Views: 21650

Answers (3)

user2227553
user2227553

Reputation:

ok My view looks like this:

    <?php

class Form extends CI_Controller {

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

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

            $this->form_validation->set_rules('name', 'Imie', 'required|min_length[5]|max_length[25]|required|alpha'); 
            $this->form_validation->set_rules('surname', 'Nazwisko', 'required|min_length[5]|max_length[25]|required|alpha'); 
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email'); 
            $this->form_validation->set_rules('number', 'Numer telefonu', 'required|alpha_numeric|max_length[10]'); 
            $this->form_validation->set_rules('brand', 'Marka', 'required|alpha'); 
            $this->form_validation->set_rules('model', 'Model', 'required|alpha'); 
            $this->form_validation->set_rules('year', 'Rok produkcji', 'required|alpha_numeric|max_length[5]'); 
            $this->form_validation->set_rules('km', 'Ilosc KM', 'required|alpha_numeric|max_length[5]'); 
            $this->form_validation->set_rules('licenceseplate', 'Tablica rejestracyjna', 'required|max_length[15]'); 
            $this->form_validation->set_rules('description', 'Opis', 'required|max_length[300]'); 
            $this->form_validation->set_rules('city', 'Miasto', 'required|max_length[30]'); 

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

        }
        else
        {
            /*$this->load->view('formsuccess');*/
            $this->load->model ( 'form' );
            $this->form->save($data);
        }

    }


}
?>

my model

    class form extends Model{

  function form(){
    parent::Model();
    $this->load->helper('url');             
  }

public function save($data) {
   $this->db->set($data);
   $this->db->insert(form);
   $this->db->insert_id();

 }



}

table in date base name form

I have no idea how you want it

Upvotes: 1

It worked yesterday.
It worked yesterday.

Reputation: 4617

After the form validation you can save data

In your controller

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

your_model.php

 public function save($data) {
   $this->db->set($data);
   $this->db->insert(table name here);
   $this->db->insert_id();
 }

Upvotes: 2

Ganesh RJ
Ganesh RJ

Reputation: 942

You have to send the data to the model

There you can use active record or normal query to save the data

It must be noted you need to include the model in the controller

$this->load->model ( 'Your model name' ); this should be included in the controller

$this->yourmodelname->functioninmodel this is the controller function call to the model

Upvotes: 0

Related Questions