Warning: mysqli_query (): Empty query - PHP MySQL

1

I have a table where I show all the cabins that I have in my database. Each one of them will have its ID, its file to modify (data of each cabin) and a "checkbox" button to be able to select those that they want to eliminate.

1) When I select one or several, and click on "Delete", I get the following error:

Warning: mysqli_query(): Empty query in C:\xampp\htdocs\daw\panel_administrador.php on line 96

2) I want that when we delete the selected ones automatically the table is updated.

HTML code with PHP:

<?php
    require_once "Clases/BD.php";
    require_once "Clases/Cabanas.php";
    require_once "conexion.php";

    //Iniciar una nueva sesión o reanudar la existente.
    session_start();
    //Si existe la sesión "administrador"..., la guardamos en una variable.
    if (isset($_SESSION['administrador'])){
        $administrador = $_SESSION['administrador'];
    }
?>

<!DOCTYPE html>
<html lang="es">
    <head>
        <meta charset="UTF-8">
        <title>Panel del administrador</title>
    </head>
    <body>
        <div id="sesion_administrador">
            <?php 
            if(isset($_SESSION['administrador'])){
                echo "Bienvenido ".$administrador."&nbsp;&nbsp;&nbsp;";
                echo "<a href='salir_administrador.php?salir=1'>Salir</a>"; //GET
                //_REQUEST = $_POST o $_GET
                if(isset($_REQUEST["salir"])){
                    unset($_SESSION["administrador"]);
                    header("Refresh:0; url=iniciar_sesion_administrador.php");
                }
            }
            ?>
        </div>

        <form action="<?php echo $_SERVER['PHP_SELF'];?>" name="tabla_datos_cabana" id="tabla_datos_cabana" method="POST">
            <div id="mostrar_cabanas">
            <br/><br/><br/>
                <table class="table table-striped" name="tabla" width="600" border="2" cellspacing="3" cellpadding="3" style="font-size: 10pt">
                    <center>
                        <tr>
                            <thead style="background-color:#A9F5A9">
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>ID Cabaña</center></b></font>
                                </td>
                                <td width=\"250\">
                                    <font face="verdana" color="blue"><b><center>Nombre</center></b></font>
                                </td>
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>Modificar</center></b></font>
                                </td>
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>Eliminar</center></b></font>
                                </td>
                            </thead>
                        </tr>   
                        <?php
                        $datos = BD::obtenerCabanas();
                        foreach($datos as $cabana){
                            echo "<tr>";
                                echo "<td width=\"150\"><font face=\"verdana\"><font size='2'><center>".$cabana->getIdcabana()."</center></font></font></td>";
                                echo "<td width=\"250\"><font face=\"verdana\"><font size='2'><center>".$cabana->getNombre()."</center></font></font></td>";
                                echo "<td width=\"150\"><font face=\"verdana\"><font size='2'><center>"?> 
                                    <a href="modificar.php?idcabana=<?=$cabana->getIdcabana();?>">
                                        <img src="imagenes/modificar.png" height='24' width='26' onmouseover="this.src='imagenes/modificar_in.png';" onmouseout="this.src='imagenes/modificar.png';">
                                    </a> <?php "</center></font></font></td>";
                                echo "<td width=\"150\"><center><input type='checkbox' name='marcados[]' id='marcados[]' value=".$cabana->getIdcabana()."></center>";
                         echo "</tr>";
                        }
                        ?>
                    </center>
                </table>

                <!-- Botón NUEVA cabaña -->
                <div class="boton_anadir" class="table-responsive" align="left">
                    <font face="verdana">
                        <b><input type="submit" style="width:200px; height:28px;" name="nueva_cabana" id="nueva_cabana" value="Añadir cabaña"></b>
                    </font><br/>
                </div>

                <!-- Botón ELIMINAR cabaña/s -->
                <div class="boton_eliminar" class="table-responsive" align="left">
                    <font face="verdana">
                        <b><input type="submit" style="width:200px; height:28px;" name="eliminar_cabanas" id="eliminar_cabanas" onclick="return confirm('¿Deseas realmente eliminar estas cabañas?');" value="Eliminar cabañas"></b>
                    </font><br/>
                </div>

                <?php
                //Si pulsamos el botón "Eliminar cabañas"...
                if(isset($_POST['eliminar_cabanas'])){
                    if(empty($_POST['marcados'])){
                        echo "<h4><center>No se ha seleccionado ninguna cabaña.</center></h4>";
                    }else{
                        foreach($_POST['marcados'] as $valor){
                            $conexion = mysqli_connect("localhost", "root", "root", "osmarrural");
                            $sql = mysql_query("DELETE FROM cabanas WHERE idcabana=".$valor);
                            $resultado = mysqli_query($conexion, $sql);
                        }
                        echo "<meta http-equiv=\"refresh\" content=\"0; URL=panel_administrador.php\">";
                    }
                }
            ?>
            </div>
        </form>
    </body>
</html>

    
asked by omaza1990 28.11.2017 в 22:54
source

2 answers

1

The mysqli_query function expects the connection as parameters and then the Query is a String and what is happening to it is a Boolean value TRUE O FALSE which are the possible return values of mysql_query ()

$sql = mysql_query("DELETE FROM cabanas WHERE idcabana=".$valor);//retorna true o false
$resultado = mysqli_query($conexion, $sql); // error

The above is the possible failure to show the error message. But it is also mixing two extensions of the language, MYSQL that it is recommended to stop using for reasons that it was declared obsolete and MySQLI that uses it but in an insecure way to directly pass the concatenated parameters. It would be better to use prepared queries Question and Answers on this topic.

Example

$mysqli = new mysqli_connect("localhost", "root", "root", "osmarrural");
$stmt = $mysqli->prepare("DELETE FROM cabanas WHERE idcabana = ?");
$stmt->bind_param('i', $valor);
$stmt->execute(); 
$stmt->close();
    
answered by 28.11.2017 / 23:08
source
1

The problem you have, is in this section:

94    $conexion = mysqli_connect("localhost", "root", "root", "osmarrural");
95    $sql = mysql_query("DELETE FROM cabanas WHERE idcabana=".$valor);
96    $resultado = mysqli_query($conexion, $sql);

First

You make the connection, until here everything is fine

$conexion = mysqli_connect("localhost", "root", "root", "osmarrural");

But

In this part we find a small detail:

$sql = mysql_query("DELETE FROM cabanas WHERE idcabana=".$valor);
$resultado = mysqli_query($conexion, $sql);

You save a value in the variable $sql , which for this case, is what returns [mysql_query()][1] .

mysql_query("DELETE FROM cabanas WHERE idcabana=".$valor);

This will return something that in PHP is known as a value [mixed][1] , which is no longer a string.

Then, you are passing certain parameters to the function [mysqli_query()][1] , which as a second parameter receives a text, but not a value the result of another function.

mysqli_query($conexion, $sql);

Solution

Organize the code as follows:

$conexion = mysqli_connect("localhost", "root", "root", "osmarrural");
$sql = sprintf("DELETE FROM cabanas WHERE idcabana='%d'", $valor);

$resultado = mysqli_query($conexion, $sql);

You can use the sprintf () function to add variables using modifiers.

    
answered by 28.11.2017 в 23:00