Checkboxs selected from MySQL database

0

I have the following error:

Fatal error: Uncaught Error: Call to undefined function mysql_query()

Line 37:

$sql = mysql_query("INSERT INTO cabanasaccesorios VALUES (".$idcabana.", ".$value.")");

Explanation:

I have a form where I want to save the selected checkboxes in a table in a MySQL database.

PHP Code:

$con = mysqli_connect("localhost", "root", "root", "osmarrural");
        if (mysqli_connect_errno()){
            echo "Error en la conexión a MySQL: " .mysqli_connect_error();
        }
        mysqli_query($con, "INSERT INTO cabanas (nombre, capacidad, descripcion, precio) VALUES ('".$nombre."', ".$capacidad.", '".$descripcion."', ".$precio.")");

        //Autogeneramos el siguiente ID a la anterior consulta/registro.
        $idcabana = mysqli_insert_id($con);

        //Guardamos los checkboxs seleccionados.
        if($_POST["accesorios"] != ""){
            if(is_array($_POST["accesorios"])){
                //Realizamos la búsqueda/ciclo.
                while(list($key, $value) = each ($_POST["accesorios"])){
                    $sql = mysql_query("INSERT INTO cabanasaccesorios VALUES (".$idcabana.", ".$value.")");
                }
            }
        }

HTML Code:

       <b>Accesorios:</b><br/>
            <label for="secador">Secador:</label>
            <input type="checkbox" id="secador" name="accesorios[]" value="1" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label for="calefaccion">Calefaccion: </label>
            <input type="checkbox" id="calefaccion" name="accesorios[]" value="2" />
            <br/><br/>
            <label for="jacuzzi">Jacuzzi: </label>
            <input type="checkbox" id="jacuzzi" name="accesorios[]" value="3" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label for="tv">TV: </label>
            <input type="checkbox" id="tv" name="accesorios[]" value="4" />
            <br/><br/>
            <label for="internet">Internet: </label>
            <input type="checkbox" id="internet" name="accesorios[]" value="5" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label for="microondas">Microondas: </label>
            <input type="checkbox" id="microondas" name="accesorios[]" value="6" />

Even though I got the previous error, is the code used to store the accessories good or is it incorrect?

I add a picture of the bbdd so you can see the tables:

    
asked by omaza1990 11.12.2017 в 12:30
source

1 answer

1

It should be mysqli_query instead of mysql_query, if you are using PHP 7 the mysql extension does not exist anymore

Where it says:

while(list($key, $value) = each ($_POST["accesorios"])){
    $sql = mysql_query("INSERT INTO cabanasaccesorios VALUES (".$idcabana.", ".$value.")");
}

must say

while(list($key, $value) = each ($_POST["accesorios"])){
    $sql = mysqli_query("INSERT INTO cabanasaccesorios VALUES (".$idcabana.", ".$value.")");
}

And I suggest you use prepared statements . That of interpolating the variables directly leaves you exposed to an SQL injection

Example with prepared sentence:

Instead of

$sql = mysqli_query("INSERT INTO cabanasaccesorios VALUES (".$idcabana.", ".$value.")");

you can use (assuming that $idcabana and $value are integers).

$stmt = $con->prepare("INSERT INTO cabanasaccesorios VALUES (?,?);");
$stmt->bind_param('ii', $idcabana, $value);
$stmt->execute();

When using bind_param, the first parameter specifies the type of variables you want to bin (in this case ii is "integer integer").

    
answered by 12.12.2017 / 12:14
source