How to delete a specific record? Php Mysql

0

I have two tables with the following structures:

Table: photos,

id_fot int (11) not null auto_increment (primary key)

user varchar (200)

varchar route (200)

description varchar (200)

Table: users,

id int (11) not null auto_increment (primary key)

user varchar (100) not null

name varchar (100) not null

email varchar (100) not null

contrasena varchar (100) not null

MY PROBLEM IS THAT WHEN SELECTING THE ICON TO ELIMINATE IT ELIMINATES ME BUT THE FIRST RECORD NOT WHAT I SELECT, IF I WANT TO ELIMINATE FOR EXAMPLE THE IMAGE 3, THE 1 IS ELIMINATED, I DO NOT KNOW HOW TO ELIMINATE ONLY THE IMAGE THAT THE USER SELECTS (IN THIS CASE THE REGISTRATION)

This is gallery.php.

<?php
session_start();
require'funcs/conexion.php';

if(!isset($_SESSION["id_usuario"])){
   header("Location: index.php");
}

?>

<html lang="es">
<head>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-theme.css" rel="stylesheet">
    <link href="css/personalizar.css" rel="stylesheet">
    <script src="js/jquery-3.1.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>

</head>

<?php
$use = $_SESSION['id_usuario'];

$sql = "SELECT * FROM usuarios WHERE id = '$use'";
$res = $mysqli->query($sql);
$row = $res->fetch_array(MYSQLI_ASSOC);
?>

<?php

require 'funcs/conexion.php';

$us = $mysqli->real_escape_string($_SESSION['id_usuario']); 

$fo = "SELECT * FROM fotos WHERE usuario = '$us'";
$res = $mysqli->query($fo);

?>

<table class="table table-sm table-dark">
            <thead>
                 <tr>
                        <th scope="col">Imagen</th>
                        <th scope="col">Descripcion</th>

                    </tr>
            </thead>
        <tbody>
            <?php while($ro = $res->fetch_array(MYSQLI_ASSOC)) { ?>
            <tr>

                <td><img src="<?php echo utf8_encode($ro['ruta']); ?>" width='200' height='200' 'image-align:center' /></td>
                <td><?php echo $ro['descripcion']; ?></td>
            <td><a href = "#" data-href = "borra.php?id_usuario=<?php echo $ro['usuario']; ?>" data-toggle = "modal" data-target = "#confirm-delete"><span class = "glyphicon glyphicon-trash"></span></a></td>                 

        </tr>
            <?php } ?>

        </tbody>
        </table>

  <!-- Modal -->
    <div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Eliminar Registro</h4>
                </div>

                <div class="modal-body">
                    ¿Desea eliminar este registro?
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-danger btn-ok">Delete</a>
                </div>
            </div>
        </div>
    </div>

    <script>
        $('#confirm-delete').on('show.bs.modal', function(e) {
            $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));

            $('.debug-url').html('Delete URL: <strong>' + $(this).find('.btn-ok').attr('href') + '</strong>');
        });
    </script>   

</body>

This is the borra.php:

 <?php
session_start();
require'funcs/conexion.php';

if(!isset($_SESSION["id_usuario"])){
   header("Location: index.php");
}

$us = $_GET['id_usuario'];

$sele = "SELECT * FROM fotos WHERE usuario = '$us'";
$res = $mysqli->query($sele);
$muestra = $res->fetch_array(MYSQLI_ASSOC);

$des = $muestra['id_fot'];

$sql = "DELETE FROM fotos WHERE id_fot = '$des'";
$resultado = $mysqli->query($sql);

?>

<html lang="es">
<head>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/bootstrap-theme.css" rel="stylesheet">
    <script src="js/jquery-3.1.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script> 
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="row" style="text-align:center">
            <?php if($resultado)  { unlink($muestra['ruta']);header("Refresh: 0.5; galeria.php?id_usuario=".$_SESSION['id_usuario']);
  ?>

 <h3>REGISTRO ELIMINADO</h3>
            <?php } else {  header("Refresh: 0.5; galeria.php?id_usuario=".$_SESSION['id_usuario']); ?>
            <h3>ERROR AL ELIMINAR</h3>
            <?php } ?>



            </div>
        </div>
    </div>
</body>

I hope someone can help me.

Greetings.

    
asked by Noctis 22.11.2018 в 16:40
source

1 answer

0

I think the problem is here:

$sele = "SELECT * FROM fotos WHERE usuario = '$us'";
$res = $mysqli->query($sele);
$muestra = $res->fetch_array(MYSQLI_ASSOC);

$des = $muestra['id_fot'];

$des will always get the first value of fotos for the user $us because there is no way to know what choice the user made. Have you tried sending the id_fot directly from galeria.php ? So you could eliminate it directly without doing this extra step.

It would be worthwhile for you to see how to avoid SQL injection because your code is vulnerable, even if this is a project you could use it. How to avoid SQL injection in PHP?

    
answered by 23.11.2018 / 04:10
source