• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding data to the database

#1
My data is not going into the table in my database. I am just getting null entries.

This is my model:
Code:
[php]  <?php
  
  class Site_model extends CI_Model{
   /*
      function getAll(){
          $q = $this->db->get('test');
      
      
      if($q->num_rows() > 0){
      
          foreach ($q->result() as $row){
          
              $data[] = $row;  
              
          }
      
          
                return $data;  
      
        }
          
    }
    */
    function get_records()
    {
        $query = $this->db->get('data');
        return $query->result();
    }
    
    function add_record($data)
    {
        $this->db->insert('data', $data);
        return;
    }
    
    function update_record($data)
    {
        $this->db->where('id',1);
        $this->db->update('data', $data);
    }
    
    function delete_row()
    {
        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data');
    }
      

  
}
[/php]

This is my controller:

Code:
[php]
<?php

class Site extends CI_Controller{

    function index()
    {
    
        //$data['myValue'] = "Some string";
        //$data['anotherValue'] = "Another string";
        
        /*$this->load->model('data_model');
        $data['row'] = $this->data_model->getAll();
        //$this->site_model->getAll();
        $this->load->view('home', $data);
        */
        $this->load->view('options_view');
        
        
    }
    
    function create()
    {
    $data = array(
    'title' => $this->input->post('title'),
    'contents' => $this->input->post('contents')
        );
        
        $this->site_model->add_record($data);
        
        $this->index();
    }
    
}
[/php]

This is my view:
Code:
[php]
<!DOCTYPE html>

<html lang = "en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled</title>
<style type="text/css" media="screen">
label {display:block;}
</style>
</head>
<body>
    <h2>Create</h2>
    <?php echo form_open('site/create');  ?>
    
    <p>
        <label for="title">Title</label>
        <input type="text" id="title" id="title" />
    </p>
    
    <p>
        <label for="title">Content:</label>
        <input type="text" name="contents" id="contents" />
    </p>
    
    <p>
        <input type="submit" id="contents" />
    </p>
    <?php echo form_close(); ?>
</body>
</html>
[/php]
Reply

#2
Hi Ben. Are you autoloading the database library and your model file? I don't autoload on my own system, so when I copied your code to my system, I could only get it to work after inserting '$this->load->database; and '$this->load->model('site_model') at the appropriate places. If you are not autoloading, consider using constructor methods in your controller and model files. Do you have the docs for Codeigniter? They explain how to do that.

Once I added those lines, I was able to add entries to the table. However, there was still one small error. In your view file, you have
Code:
<label for="title">Title</label>
<input type="text" id="title" id="title" />

Because that textbox has no name attribute, the contents never get sent to Codeigniter via the $_POST variable, so when you go 'title' => $this->input->post('title') , you get FALSE returned instead of the title.

Is your PHP set up to report errors to the screen? Ideally, your development system would have error reporting turned on to the max in your PHP.INI file. The line looks like error_reporting = E_ALL.

Finally, you're using the same id="content" on two different controls. That's not causing your problem, but it might cause styling problems. The id attribute must be unique, used only once.

Let me know if the above solves your problem. Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply

#3
Thanks for your quick reply RobertSF,

I changed my view, based on your observations, and ran the project. I was able to update my db. I'd like to show you my autoloading file so you can see what I am autoloading:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| AUTO-LOADER
| -------------------------------------------------------------------
| This file specifies which systems should be loaded by default.
|
| In order to keep the framework as light-weight as possible only the
| absolute minimal resources are loaded by default. For example,
| the database is not connected to automatically since no assumption
| is made regarding whether you intend to use it. This file lets
| you globally define which systems you would like loaded with every
| request.
|
| -------------------------------------------------------------------
| Instructions
| -------------------------------------------------------------------
|
| These are the things you can load automatically:
|
| 1. Packages
| 2. Libraries
| 3. Helper files
| 4. Custom config files
| 5. Language files
| 6. Models
|
*/

/*
| -------------------------------------------------------------------
| Auto-load Packges
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['packages'] = array(APPPATH.'third_party', '/usr/local/shared');
|
*/

$autoload['packages'] = array();


/*
| -------------------------------------------------------------------
| Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your application/libraries folder.
|
| Prototype:
|
| $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

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


/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/

$autoload['helper'] = array('url','form');//parameters url, form added 1/20/15


/*
| -------------------------------------------------------------------
| Auto-load Config files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['config'] = array('config1', 'config2');
|
| NOTE: This item is intended for use ONLY if you have created custom
| config files. Otherwise, leave it blank.
|
*/

$autoload['config'] = array();


/*
| -------------------------------------------------------------------
| Auto-load Language files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['language'] = array('lang1', 'lang2');
|
| NOTE: Do not include the "_lang" part of your file. For example
| "codeigniter_lang.php" would be referenced as array('codeigniter');
|
*/

$autoload['language'] = array();


/*
| -------------------------------------------------------------------
| Auto-load Models
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['model'] = array('model1', 'model2');
|
*/

$autoload['model'] = array('site_model');


/* End of file autoload.php */
/* Location: ./application/config/autoload.php */

Can you tell me where '$this->load->database' and '$this->load->model('site_model') ' should be placed? I am very new to this.

Thanks, Ben
Reply

#4
(02-17-2015, 11:55 AM)Ben Wrote: Thanks for your quick reply RobertSF,

$autoload['libraries'] = array('database' );//param 'database' added
$autoload['model'] = array('site_model');

Can you tell me where  '$this->load->database' and '$this->load->model('site_model') ' should be placed? I am very new to this.

You're welcome, and I'm glad your application started to work. Since you are autoloading the necessary libraries and models, you don't need to use $this->load->database, etc. Autoloading does that for you.

But then why wasn't the application working? I suspect you set up your database to require data in all fields, so when you submitted the entry without a title, the database rejected it. Turning on errors should let you know next time when it happens.

But if you didn't use autoload, where would you place the $this->load statements? The answer is anywhere before you actually used the functionality loaded. You would have to use $this->load->database before you used $this->db->get('data'), for example. However, the place where you would generally place them is in the class constructor. The constructor is a special function that runs when you create a new instance of a class. Your controllers and models are classes.

Codeigniter creates instances of your controllers and models behind the scenes, so you don't have to. Otherwise, without Codeigniter, you would have to use code like $site_model = New Site_model to create an instance of Site_model and then use code like $site_model->add_record($data) to write a new record to your database.

Codeigniter does not automatically create constructor functions for you, but if you are autoloading libraries, models, etc., you don't have to use constructors. The purpose of the constructor is to initialize the object created from the class file with specific data or to set it up to start working. If autoloading does that for you, then there's no need to create constructors.[/font][/color]

But suppose you had a particular library that you used only with one controller, and all your other controllers didn't use it. In that case, it would make sense to load that library for that one controller through that controller's constructor. Why? Because if you autoload it, it autoloads for every controller, whether it needs it or not, and that's kind of a waste of time, memory, etc.

Consider spending some time going over the PHP documentation as well as looking up some PHP tutorials about classes, objects, and methods. Learning a little about how it's done from scratch, that is to say, without Codeigniter, will put you on firmer ground and give you a greater appreciation for Codeigniter. Pretty soon you won't be so new at this. Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.