我有这个应用程序来管理书籍数据库,添加,删除,更新和获取整个列表。 插入重复的条目或删除不存在的条目时,我没有任何错误:
怎么了?
mysqli_report(MYSQLI_REPORT_ERROR / MYSQLI_REPORT_STRICT);
require "conexion_libreria.php";
class Libreria extends ConexionLibreria {
public function consultarLibros() {
try {
$sql = "SELECT * FROM libros ORDER BY titulo";
$resultado = $this->conexionLibros->query($sql);
$libros = $resultado->fetch_all(MYSQLI_ASSOC);
return $libros;
} catch (Exception $e) {
throw new Exception("Hubo un error en el select: ".$e->getMessage());
}
}
public function altaLibros($titulo, $precio) {
try {
$sql = "INSERT INTO libros VALUES(NULL, '$titulo', '$precio')";
$this->conexionLibros->query($sql);
return "Alta efectuada";
} catch (Exception $e) {
if ($e->getCode() == '1062') {
throw new Exception("Libro ya existe en la base de datos");
} else {
throw new Exception("Error en el alta: ".$e->getCode().'/'.$e->getFile().'/'.$e->getLine().'/'.$e->getMessage());
}
}
}
public function bajaLibros($id) {
try {
$sql = "DELETE FROM libros WHERE idlibros='$id'";
$this->conexionLibros->query($sql);
return "Baja efectuada";
} catch (Exception $e) {
if ($e->getCode() == '1062') {
throw new Exception("Libro no existe en la base de datos");
} else {
throw new Exception("Error en la baja: ".$e->getCode().'/'.$e->getFile().'/'.$e->getLine().'/'.$e->getMessage());
}
}
}
public function modificaLibros($id, $titulo, $precio) {
try {
$sql = "UPDATE libros SET idlibros='$id', titulo='$titulo', precio='$precio' WHERE idlibros='$id'";
$this->conexionLibros->query($sql);
return "Modificación efectuada";
} catch (Exception $e) {
if ($e->getCode() == '1062') {
throw new Exception("Libro no existe en la base de datos");
} else {
throw new Exception("Error en la modificación: ".$e->getCode().'/'.$e->getFile().'/'.$e->getLine().'/'.$e->getMessage());
}
}
}
}
conexion_libreria.php
<?php
class ConexionLibreria {
protected $conexionLibros;
public function __construct() {
try {
$this->conexionLibros = new mysqli('localhost', 'root', '', 'libreria');
$this->conexionLibros->set_charset('utf8');
} catch (Exception $e) {
echo $e->getMessage();
}
}
}