删除客户端但不删除图像

问题描述 投票:-1回答:1

删除客户端时,除了存储在文件夹/ view / cliente / imagenes中的图像之外,其所有信息都将被删除

模型

<?php
    class cliente{
        private $pdo;
        public $id;
        public $res;
        public $resultado;
        public $row;
        public $dni;
        public $Nombre;
        public $Apellido;  
        public $Correo;
        public $Telefono;
        public $imagen;

        public function __CONSTRUCT(){
            try{
                $this->pdo = conectar();
            }
            catch(Exception $e){
                die($e->getMessage());
            }
        }
        public function Listar(){
            try{
                $result = array();
                $stm = $this->pdo->prepare("SELECT * FROM cliente");
                $stm->execute();
                return $stm->fetchAll(PDO::FETCH_OBJ);
            }
            catch(Exception $e){
                die($e->getMessage());
            }
        }
        public function Buscarme($Nombre){
            try{
                $stm = $this->pdo->prepare("SELECT * FROM cliente WHERE Nombre LIKE '%".$Nombre."%'");
                $stm -> execute();  
                $res = $stm->fetchAll(PDO::FETCH_ASSOC); 
                $this->resultado= array();
                $i=0;
                foreach($res as $row){
                    $this->resultado[$i]['id']          = $row['id'];
                    $this->resultado[$i]['dni']         = $row['dni'];
                    $this->resultado[$i]['Nombre']      = $row['Nombre'] ;
                    $this->resultado[$i]['Apellido']    = $row['Apellido'];
                    $this->resultado[$i]['Correo']      = $row['Correo'] ;
                    $this->resultado[$i]['Telefono']    = $row['Telefono'] ;
                    $this->resultado[$i]['Imagen']      = $row['Imagen'] ;
                    $i++;
                }
            }
            catch (Exception $ex) {
                die($e->getMessage());
            }
        }
        public function ListarApellido(){
            try{
                $result = array();
                $stm = $this->pdo->prepare("SELECT * FROM cliente WHERE Apellido = 'Ardila'");
                $stm->execute();
                return $stm->fetchAll(PDO::FETCH_OBJ);
            }
            catch(Exception $e){
                die($e->getMessage());
            }
        }
        public function ListarApellidoM(){
            try{
                $result = array();
                $stm = $this->pdo->prepare("SELECT * FROM cliente WHERE Apellido = 'Mateus'");
                $stm->execute();
                return $stm->fetchAll(PDO::FETCH_OBJ);
            }
            catch(Exception $e){
                die($e->getMessage());
            }
        }
        public function Obtener($id){
            try{
                $stm = $this->pdo->prepare("SELECT * FROM cliente WHERE id = ?");
                $stm->execute(array($id));
                return $stm->fetch(PDO::FETCH_OBJ);
            }
            catch (Exception $e){
                die($e->getMessage());
            }
        }
        public function Eliminar($id){
            try{
                $stm = $this->pdo->prepare("DELETE FROM cliente WHERE id = ?");
                $stm->execute(array($id));
            }
            catch (Exception $e){
                die($e->getMessage());
            }
        }
        public function Actualizar(cliente $data){
            try{
                $sql = "UPDATE cliente SET dni = ?, Nombre = ?, Apellido = ?, Correo = ?, Telefono = ?, Imagen = ? WHERE id = ?";
                $this->pdo->prepare($sql)->execute(array( $data->dni, $data->Nombre,$data->Apellido,$data->Correo,$data->telefono, $data->imagen, $data->id));
            }
            catch (Exception $e){
                die($e->getMessage());
            }
        }
        public function Registrar(cliente $data){
            try{
                $sql = "INSERT INTO cliente (dni,Nombre,Apellido,Correo,telefono,Imagen) VALUES (?, ?, ?, ?, ?, ?)";
                $this->pdo->prepare($sql)->execute(array($data->dni, $data->Nombre,$data->Apellido, $data->Correo, $data->telefono,$data->imagen));
            }
            catch (Exception $e){
                die($e->getMessage());
            }
        }
    }
?>

调节器

<?php
    include_once 'model/cliente.php';
    class clienteController {
        private $model;
        public $mensaje;
        public function __CONSTRUCT() {
            $this->model = new cliente();
        }
        public function Paginar() {
            require_once 'view/header.php';
            require_once 'view/cliente/cliente.php';
        }
        public function agregar(){
            $cliente = new cliente();
            if (isset($_REQUEST['id'])) {
                $cliente = $this->model->Obtener($_REQUEST['id']);
            }
            require_once 'view/header.php';
            require_once 'view/cliente/cliente-editar.php';
        }
        public function buscar() {
            require_once 'view/header.php';
            require_once 'view/cliente/cliente.php';
            $cliente = new cliente();
            if (isset($_POST['Nombre'])) {
                $cliente = $this->model->Buscarme($_POST['Nombre']);
            }
        }
        public function redireccionar() {
            require_once 'view/header.php';
            if ($_REQUEST['Nombre'] == "pablo") {
                require_once 'view/creditos/creditos.php';
            }
            if ($_REQUEST['Nombre'] == "ives") {
                require_once 'view/creditos/creditos2.php';
            }
        }
        public function listador() {
            require_once 'view/header.php';
            if ($_REQUEST['Nombre'] == "Ardila") {
                require_once 'view/cliente/apellidos/Ardila.php';
            }
            if ($_REQUEST['Nombre'] == "Mateus") {
                require_once 'view/cliente/apellidos/Mateus.php';
            }
        }
        public function Guardar() {
            require_once 'view/header.php';
            $cliente = new cliente();
            $tamaño=$_FILES['imagen']['size'];
            $tipo=$_FILES['imagen']['type'];
            if($tamaño <= 3000000){
                if($tipo=="image/jpeg"||$tipo=="image/jpg" || $tipo=="image/png" || $tipo=="image/gif"){
                    $ruta = "view/cliente/imagenes/";
                    opendir($ruta);
                    $destino = $ruta . $_FILES['imagen']['name'];
                    copy($_FILES['imagen']['tmp_name'], $destino);
                    $cliente->id = $_REQUEST['id'];
                    $cliente->dni = $_REQUEST['dni'];
                    $cliente->Nombre = $_REQUEST['Nombre'];
                    $cliente->Apellido = $_REQUEST['Apellido'];
                    $cliente->Correo = $_REQUEST['Correo'];
                    $cliente->telefono = $_REQUEST['telefono'];
                    $cliente->imagen = $_FILES['imagen']['name'];
                    if ($cliente->id > 0) {
                        $this->model->Actualizar($cliente);
                    } else {
                        $this->model->Registrar($cliente);
                    }
                    header('Location: index.php');
                }
                else{
                    $this->mensaje="FORMATO NO ADMITIDO";
                    require_once 'view/cliente/imagenes/error.php';
                }
            }
            else{
                $this->mensaje="TAMAÑO EXEDIDO";
                require_once 'view/cliente/imagenes/error.php';
            }
        }
        public function Eliminar() {
            $this->model->Eliminar($_REQUEST['id']);
            unlink($filename);
            header('Location: index.php');
        }
    }
?>

Eliminar方法获取id,删除客户端但不删除与该客户端关联的/ view / cliente / imagenes文件夹中的jpg文件。

创建客户端时,必须加载图像,只将图像名称存储在数据库中,图像存储在文件夹/ view / client / images中

客户数据库

CREATE TABLE cliente (
    id int(11) NOT NULL,
    dni int(60) NOT NULL,
    Nombre varchar(50) NOT NULL DEFAULT '0',
    Apellido varchar(50) NOT NULL DEFAULT '0',
    Correo varchar(50) NOT NULL,
    Telefono varchar(60) NOT NULL,
    Imagen text COLLATE utf8_spanish_ci NOT NULL
);
ALTER TABLE cliente ADD PRIMARY KEY (`id`);
ALTER TABLE cliente MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1; 
php mysql pdo
1个回答
1
投票

我不知道保存图像的方式是什么,但它应该是这样的:

public function Eliminar($id)
{
    try {
                // Get image path
                $imgPath = $this->model->Obtener($id)['Imagen'];
                // Delete image from server
                unlink($imgPath);
                $stm = $this->pdo->prepare("DELETE FROM cliente WHERE id = ?");                   
                $stm->execute(array($id));
    } catch (Exception $e) 
    {
        die($e->getMessage());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.