Warning: mysqli_query (): Empty query - PHP MySQL


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:

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

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

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

        <form action="<?php echo $_SERVER['PHP_SELF'];?>" name="tabla_datos_cabana" id="tabla_datos_cabana" method="POST">
            <div id="mostrar_cabanas">
                <table class="table table-striped" name="tabla" width="600" border="2" cellspacing="3" cellpadding="3" style="font-size: 10pt">
                            <thead style="background-color:#A9F5A9">
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>ID Cabaña</center></b></font>
                                <td width=\"250\">
                                    <font face="verdana" color="blue"><b><center>Nombre</center></b></font>
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>Modificar</center></b></font>
                                <td width=\"150\">
                                    <font face="verdana" color="blue"><b><center>Eliminar</center></b></font>
                        $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>";

                <!-- 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>

                <!-- 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>

                //Si pulsamos el botón "Eliminar cabañas"...
                        echo "<h4><center>No se ha seleccionado ninguna cabaña.</center></h4>";
                        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\">";

asked by omaza1990 28.11.2017 в 22:54

2 answers


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.


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

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);


You make the connection, until here everything is fine

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


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);


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