Insert enum field (mysql) multiple checkbox values

0

I need help to insert multiple values in mysql of a checkbox in a form:

  • I have these checkboxes in my form

<LABEL>Extras (los que procedan):</LABEL>
<INPUT TYPE="checkbox" NAME="extras[]" VALUE="Vista Al Mar">Vista al Mar
<INPUT TYPE="checkbox" NAME="extras[]" VALUE="Piscina">Piscina
<INPUT TYPE="checkbox" NAME="extras[]" VALUE="Jardin">Jardin
<input type="checkbox" NAME="extras[]" VALUE="Quincho">Quincho
<INPUT TYPE="checkbox" NAME="extras[]" VALUE="Estacionamiento">Estacionamiento
  • In my MySql table of the form I have that column as

extras = ENUM ('Ocean View', 'Pool', 'Garden', 'Quincho', 'Parking')

  • This is my php code where I insert the form:

<?php

include("../conexion.php");  


 insertarVivienda($_POST['tipo'], $_POST['zona'], $_POST['direccion'], $_POST['ndorm'],$_POST['precio'],$_POST['tamano'],$_POST['extras[]'],$_POST['foto'],
                     $_POST['observaciones'],$_POST['tipo_servicio']);

function insertarVivienda($tipo,$zona,$direccion,$ndormitorios,$precio,$tamano,$extras,$foto,$observaciones,$tipo_servicio)
{
echo $query = "INSERT INTO 'vivienda' ('idvivienda', 'tipo_vivienda', 'zona', 'direccion', 'ndormitorios', 'precio', 'tamano', 'extras', 'foto', 'observaciones', 'usuario_id',tipo_servicio) VALUES ('Null', '".$tipo."', '".$zona."', '".$direccion."', '".$ndormitorios."', '".$precio."', '".$tamano."', '".$extras."', '".$foto."', '".$observaciones."','".$tipo_servicio.", '1')";
 $conexion = conectar();
 mysqli_query($conexion,$query) or die (mysqli_error());     
}

?>
  • All other data is inserted without problems, still not worth anything and I allow empty only to see what happens, but when I select 1 or more check box throws me this error:

Notice: Undefined index: extras[] in C:\xampp\htdocs\inmobiliaria\crud\insertar.php on line 6
INSERT INTO 'vivienda' ('idvivienda', 'tipo_vivienda', 'zona', 'direccion', 'ndormitorios', 'precio', 'tamano', 'extras', 'foto', 'observaciones', 'usuario_id') VALUES ('Null', 'Departamento', 'El Tabo', '', '1', '', '', '', '', '', '1')

with everything else ok, before I had no problems when I used "extras" as combo box but I need to select more than 1 extra so I thought to use checkbox ...

thank you very much.

    
asked by Jordan Blake Told 27.05.2018 в 19:34
source

1 answer

0

What he has to do is the following, since the data is an array, what he has to do is use a foreach. The enum type can only store a single type of specific data, defined in the data base. the type of data that you want to keep that has several types of data, has a relation of much to much and that is seen in database, so you have to create a table where you have to keep: housing_id and extras_id

<?php

include("../conexion.php");  


 insertarVivienda($_POST['tipo'], $_POST['zona'], $_POST['direccion'], $_POST['ndorm'],$_POST['precio'],$_POST['tamano'],$_POST['extras'],$_POST['foto'],
                     $_POST['observaciones'],$_POST['tipo_servicio']);

function insertarVivienda($tipo,$zona,$direccion,$ndormitorios,$precio,$tamano,$extras,$foto,$observaciones,$tipo_servicio)
{

echo $query = "INSERT INTO 'vivienda' ('idvivienda', 'tipo_vivienda', 'zona', 'direccion', 'ndormitorios', 'precio', 'tamano','foto', 'observaciones', 'usuario_id',tipo_servicio) VALUES ('Null', '".$tipo."', '".$zona."', '".$direccion."', '".$ndormitorios."', '".$precio."', '".$tamano."','".$foto."', '".$observaciones."','".$tipo_servicio.", '1')";
 $conexion = conectar();
    //mysqli_query($conexion,$query) or die (mysqli_error());     
    //devuelve el id de la fila insertada 
    if(mysqli_query($conn, $query)) {
        $vivienda_id = mysqli_insert_id($conn);
        $vivienda_id;
        foreach ($extras as $extras_id) {
        $query = "INSERT INTO 'vivienda_extras' ('vivienda_id', 'extras_id') VALUES ('".$vivienda_id."', '".$extras_id."')";
            mysqli_query($conn, $sql);

        }

    }



}

?>
    
answered by 28.05.2018 в 00:52