Problems with pdo connection and prepared queries php poo

1

I get the following error:

  

Fatal error: Uncaught Error: Call to a member function prepare () on   null in C: \ xampp \ htdocs \ EPWeb \ Admin \ Business \ CapaNegocios.php: 17 Stack   trace: # 0 C: \ xampp \ htdocs \ EPWeb \ Admin \ Presentation \ Units.php (18):   Unit-> InsertUnity (1, 'z', 'a', 'a', 1) # 1 {main} thrown in   C: \ xampp \ htdocs \ EPWeb \ Admin \ Business \ CapaNegocios.php on line 17

I'm learning, my code is this:

<?php
class Conexion{

    protected $conexion_db;

    public function Conectar()
    {
        try{

            $this->conexion_db= new PDO('mysql:host=localhost; dbname=bd_espamep','root','123456');
            $this->conexion_db-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conexion_db->exec("SET CHARACTER SET utf8"):

                return $this->conexion_db;





        }
        catch(PDOException $e){
            echo "La linea de error es: " . $e->getline();
        }
    }
    public function desconectar()
    {
        $this -> conexion = null;
    }
}
?>

Negocios

<?php
require_once "../Conexion/CapaDatos.php";

class Unidad extends Conexion
{

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

    public function InsertarUnidad($idempresa, $uni_nombre, $uni_objetivo, $uni_logo, $uni_eliminado)
    {
        try
        {
            $sql = "INSERT INTO unidad(idempresa, uni_nombre, uni_objetivo, uni_logo, uni_eliminado) VALUES (?,?,?,?,?)";
            $sentencia = $this->conexion_db->prepare($sql);
            $sentencia -> bind_param('isssi',$uni_nombre,$uni_objetivo, $uni_logo);

            $sentencia->execute();

            echo "Guardado";

        }catch(Exception $e){
            echo $e->getMessage();
        }
    }



}

?>

Presentation

<?php
require_once "../negocios/CapaNegocios.php";


try{
  if(!empty($_POST))
  {
    $idempresa = 1;
    $uni_eliminado = 1;
    $uni_nombre = $_POST['txtunidad'];
    $uni_objetivo = $_POST['txtobjetivou'];
    $uni_logo = $_POST['logou'];

    $objetoNegocio= new Unidad();

    if(isset($_POST["Guardar"]))
    {
      $objetoNegocio-> InsertarUnidad($idempresa, $uni_nombre, $uni_objetivo, $uni_logo, $uni_eliminado);
    }
  }
}
catch(PD0Eception $e)
{
  echo $e -> getMessage();
}

?>
    
asked by Tevin Lectong 25.06.2018 в 04:16
source

1 answer

0

The class Conexion has a method to connect to the database ( Conectar ) but that method is not called anywhere. One thing you can do is create a default constructor that calls the Conectar method so that the $conexion_db attribute has a value.

<?php
class Conexion{

    protected $conexion_db;

    public function Conectar()
    {
        try{

            $this->conexion_db= new PDO('mysql:host=localhost; dbname=bd_espamep','root','123456');
            $this->conexion_db-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conexion_db->exec("SET CHARACTER SET utf8"):

            return $this->conexion_db;

        }
        catch(PDOException $e){
            echo "La linea de error es: " . $e->getline();
        }
    }
    public function desconectar()
    {
        $this -> conexion = null;
    }

    // constructor por defecto que crea la conexión
    function __construct() {
        $this -> Conectar();
    }
}
?>

Also, in class Unidad the parent's constructor is not being called because it does not have a default constructor either. It has a CapaNegocios() method called by the parent's constructor ... but it is not called from anywhere. Add something like this:

and then it would look like this:

class Unidad extends Conexion
{

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

    public function InsertarUnidad($idempresa, $uni_nombre, $uni_objetivo, $uni_logo, $uni_eliminado)
    {
        try
        {
            $sql = "INSERT INTO unidad(idempresa, uni_nombre, uni_objetivo, uni_logo, uni_eliminado) VALUES (?,?,?,?,?)";
            $sentencia = $this->conexion_db->prepare($sql);

            // corregido con datos de los comentarios y la ayuda de A.Cedano
            $arrParams=array($idempresa, $uni_nombre, $uni_objetivo, $uni_logo, $uni_eliminado); 
            $sentencia->execute($arrParams);

            echo "Guardado";

        }catch(Exception $e){
            echo $e->getMessage();
        }
    }

    // constructor por defecto que llama al padre
    function __construct() {
        parent::__construct();
    } 

}

Try that and then $conexion_db should already have a value. Tell me if he keeps giving you the error.

    
answered by 25.06.2018 в 04:51