Welcome Guest, Not a member yet? Register   Sign In
Simple form to save data to db
#1

Hello,

I'm a couple days into CodeIgniter 3.0 now and very frustrated; hence the reason for signing up with this forum. I have been scouring the web for simple examples of how to save data to a db using CI 3.0, but can't find anything that is either CI 3.0, that actually works, or that is all inclusive.

Yes, the manual shows some examples of working with db's, but doesn't put it all together for you using M, V, & C files; at least not that I've seen.

So, I'm asking if someone would be generous enough to either point me in the direction of a tut that uses: CI 3.0, all M,V, & C, and proper security implementation (sql inject proof) for submitting a few form fields to a db.

If you don't have any link that would meet all those requests, would you consider providing simple MVC files that show me how to submit to a db:

1 textbox
1 listbox (i.e. menu select, drop down menu)
1 checkbox 
1 textarea
w/ Submit button

using one or two validations and anything else you like to use that you feel makes your submission more secure from an sql injection.

I learn very well when I'm able to see something and feel I can get a great start building on what you've given me if I just had a simple example to follow to get started with.

Thank you very much to anyone willing to take a little time to help.
Reply
#2

(This post was last modified: 04-30-2016, 10:19 AM by InsiteFX.)

You can find examples that use forms in just about all the auth system that built for CI.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Model:
PHP Code:
<?php 
class Test_model extends CI_Model {

  private 
$table 'test_table';

  public function 
get_record($id)
  {
     
$query $this->db->from($this->table)->where('id',$id)->get();
    if ( 
$query->num_rows() != ) {
       return 
$query->row_array();
    }
    else {
       return 
FALSE;
    }
  }

  public function 
save_record($id,$data)
  {
     if (
$id 0) {
      
$this->db->insert($this->table,$data);
    }
    else {
      
$this->db->update($this->table,$data,'id=' $id);
    }
  }


Controller:
PHP Code:
<?php
class Testcontroller extends CI_Controller {

  public function 
__construct() 
  {
     
parent::__construct();
     
$this->load->model('test_model');
  }

  public function 
show_testform() {
    
$this->load->helper('form');
    
$this->load->library('form_validation');
    
$this->form_validation->set_rules('username','User name','required');
    
$this->form_validation->set_rules('country','Country','required');
    
$this->form_validation->set_rules('description','Description','required');
    
$this->form_validation->set_rules('has_passport');
    if (
$this->form_validation->run() == FALSE) {
      
$data['countries'] = array(
         
'' => '(select a country)',
         
'USA' => 'USA',
         
'UK' => 'UK'
      
);
      
$this->load->view('test_form',$data);
    }
    else {
       
$id 0;
       
$data = array(
         
'username'=>$this->input->post('username'),
         
'country' => $this->input->post('country'),
         
'has_passport' => isset($this->input->post('has_passport')),
         
'description' => $this->input->post('description')
       );
       
$this->test_model->save_record($id,$data);
  }


View:
PHP Code:
<?php
echo form_open();
echo 
'User name: ' .  form_input('username',NULL) . '<br />';
echo 
'Country: ' form_dropdown('country',$countries,NULL) . '<br />';
echo 
'Has passport: ' form_checkbox('has_passport',1FALSE) . '<br />';
echo 
'Description:<br />';
echo 
form_textearea( array('rows'=>8'cols' => 60'name' =>'description') . '<br />';
echo 
form_submit('submit','Submit form');
echo 
form_close(); 

That's a basic setup based on MVC in CI.
Reply
#4

You can take a look at my weblog application (see signature) and use it as a starting point to learn more about CodeIgniter?
Reply
#5

(04-30-2016, 11:10 AM)Wouter60 Wrote: Model:
PHP Code:
<?php 
class Test_model extends CI_Model {

 
 private $table 'test_table';

 
 public function get_record($id)
 
 {
 
    $query $this->db->from($this->table)->where('id',$id)->get();
 
   if $query->num_rows() != ) {
 
      return $query->row_array();
 
   }
 
   else {
 
      return FALSE;
 
   }
 
 }

 
 public function save_record($id,$data)
 
 {
 
    if ($id 0) {
 
     $this->db->insert($this->table,$data);
 
   }
 
   else {
 
     $this->db->update($this->table,$data,'id=' $id);
 
   }
 
 }


Controller:
PHP Code:
<?php
class Testcontroller extends CI_Controller {

 
 public function __construct() 
 
 {
 
    parent::__construct();
 
    $this->load->model('test_model');
 
 }

 
 public function show_testform() {
 
   $this->load->helper('form');
 
   $this->load->library('form_validation');
 
   $this->form_validation->set_rules('username','User name','required');
 
   $this->form_validation->set_rules('country','Country','required');
 
   $this->form_validation->set_rules('description','Description','required');
 
   $this->form_validation->set_rules('has_passport');
 
   if ($this->form_validation->run() == FALSE) {
 
     $data['countries'] = array(
 
        '' => '(select a country)',
 
        'USA' => 'USA',
 
        'UK' => 'UK'
 
     );
 
     $this->load->view('test_form',$data);
 
   }
 
   else {
 
      $id 0;
 
      $data = array(
 
        'username'=>$this->input->post('username'),
 
        'country' => $this->input->post('country'),
 
        'has_passport' => isset($this->input->post('has_passport')),
 
        'description' => $this->input->post('description')
 
      );
 
      $this->test_model->save_record($id,$data);
 
 }


View:
PHP Code:
<?php
echo form_open();
echo 
'User name: '  form_input('username',NULL) . '<br />';
echo 
'Country: ' form_dropdown('country',$countries,NULL) . '<br />';
echo 
'Has passport: ' form_checkbox('has_passport',1FALSE) . '<br />';
echo 
'Description:<br />';
echo 
form_textearea( array('rows'=>8'cols' => 60'name' =>'description') . '<br />';
echo 
form_submit('submit','Submit form');
echo 
form_close(); 

That's a basic setup based on MVC in CI.

Thank you.

Can you please confirm this is working on CI 3.0 because I'm getting fatal errors and notices. Also, what were the file names?

Thank you again!
Reply
#6

All models, controllers and libraries in CI 3.x must start with a capital letter.
In my example:
application/models/Test_model.php
application/controllers/Testcontroller.php

Views are in lowercase:
application/views/test_view.php

If you load a model or library, with $this->load->model(...), then you don't use capital letters.

Information about the differences between 2.x en 3.x is here:
http://www.codeigniter.com/user_guide/in...e_300.html
Reply
#7

(04-30-2016, 04:06 PM)Code4fun Wrote: You can take a look at my weblog application (see signature) and use it as a starting point to learn more about CodeIgniter?

Thank you. 

I downloaded/installed it and only the homepage works. All other links are broken for me.
Reply
#8

(This post was last modified: 05-06-2016, 05:31 PM by Code4fun.)

Quote:I downloaded/installed it and only the homepage works. All other links are broken for me.

Edit $config['base_url'] = ''; and remove index.php from $config['index_page'] = 'index.php'; in your config file.
Reply
#9

(05-06-2016, 05:29 PM)Code4fun Wrote:
Quote:I downloaded/installed it and only the homepage works. All other links are broken for me.

Edit $config['base_url'] = ''; and remove index.php from $config['index_page'] = 'index.php'; in your config file.

..was already removed.
Reply
#10

(This post was last modified: 05-09-2016, 06:28 AM by meOmy.)

(05-06-2016, 05:31 PM)meOmy Wrote:
(05-06-2016, 05:29 PM)Code4fun Wrote:
Quote:I downloaded/installed it and only the homepage works. All other links are broken for me.

Edit $config['base_url'] = ''; and remove index.php from $config['index_page'] = 'index.php'; in your config file.

..was already removed.


Code4fun:

Hi,

I've done as you suggested with the config file in terms of base URL and index.php, but still no luck.

I'm beginning to believe the issue is perhaps tied to the base URL.

When I go to your project's homepage, your site is there, but when I place my mouse over the NEWS, CONTACT, etc links, the base URL is displayed differently/incorrectly.

I currently have: http://localhost/CI/ in my config and your homepage comes up fine

When I mouse over links stated above, I get: 

localhost/news
localhost/contact

... instead of:

localhost/CI/news
localhost/CI/contact

... which, by the way, work when I manually type them in

I'm using UniServer 11.8.2, but don't feel like that's the issue; although it might be. I just don't know.

I tried moving your files to localhost/CI/Test to see if that might work, but I get the exact same issue as stated above.

Do you have any special .htaccess or index.php configurations that I might not have? 

Hoping someone can help, so I can get started learning more about CI.

thank you!

EDIT: Also, when I add "CI" to the following menu in your theme:

{menudata}
<a class="blog-nav-item {active}" href="CI{link}">{name}</a>
{/menudata}

... I get localhost/CI/CI/news
Reply




Theme © iAndrew 2016 - Forum software by © MyBB