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();
}
?>