Undefined property and call to a member function on null in codeigniter

2

I'm doing a project with php and codeigniter but I get an error

my codes are as follows:

database.php      

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'sedeges',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

view vista_centroAcogidonuevo.php

    <html>
<body>
    <h1>Registrar nuevo centro de acogida</h1>
    <form action="centroAcogido/datos" method="POST">
        <label>Nombre del Centro :</label>
        <input type="text" placeholder="ingresar el nombre del centro" size="40" name="nombre" id="nombre" /><p>
        <label>Nombre del Administrador :</label>
        <input type="text" placeholder="nombre del administrador" size="40" name="nomb_admin" id="nomb_admin" /><p>
        <label>Direccion del centro :</label>
        <input type="text" placeholder="ingresar la direccion del centro" size="40" name="direccion" id="direccion" /><p>
        <label>Telefono :</label>
        <input type="text" placeholder="ingresar el numero de telefono" size="40" name="telefono" id="telefono" /><br/><br/>
        <input type="submit" value="Guardar Registro"/>

    </form>

</body>
<html/>

driver centroAcogido.php

   <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class centroAcogido extends CI_Controller {

 public function __construct()
        {
                parent::__construct();

                // Your own constructor code
        }

    public function index()
    {
        $this->load->view('centroAcogida/vista_centroAcogido_nuevo');
    }

    function datos()
    {
        $data=$arrayName = array('nombre' =>$this ->input ->post ('nombre'), 
                                  'administrador ' =>$this ->input ->post('nomb_admin'),
                                  'direccion' =>$this ->input ->post('direccion'),
                                  'telefono' =>$this ->input ->post('telefono')
            );
        $this->load->model('M_centroAcogido');//caraga el modelo
        $this->M_centroAcogido->crearCentro($data);//funcion crear centro (va con los valores introducidos a data)
    }
}

model M_centroAcogido.php

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class M_centroAcogido extends CI_Model {



        public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();
        }

        function crearCentro($data)
        {
            $this->db->insert('centro_acogida', array('nombre_centro'=>$data['nombre'], 
                                                    'admin_centro'=>$data['administrador'],
                                                        'dir_centro'=>$data['direccion'],
                                                        'telefono_centro'=>$data['telefono']));

        }

 }

It's the first time I use codeigniter.

    
asked by Marcos Benito Herrera 04.10.2016 в 23:46
source

1 answer

1

You have some errors:

The format for the drivers is: the controller names should always be with the first letter in upper case in your case centroAcogido should be CentroAcogido and the name of the php file should be the same, it should be CentroAcogido.php

Then in your model, unless you load the database automatically in your config.php file, you should load the db in your model like this:

public function __construct(){
  // Call the CI_Model constructor
  parent::__construct();
  $this->load->database();
}
    
answered by 05.10.2016 / 00:15
source