I have a form where a name is placed and you choose between 5 different types of "profile".
whose code is as follows:
<form id="registroForm" name="registroForm" class="ui form" role="form" action="guardar.php" method="post" autocomplete="off">
<div class="ui segment">
<div class="field">
<label>Nombre y Apellido:</label>
<input id="nombre" type="text" name="nombre" minlength=3 maxlength=100 required>
</div>
<div class="field">
<label>Tipo de Perfil:</label>
<select id="tipoUsuario" class="ui fluid search dropdown" multiple name="clase[]" required>
<option data-subtext="Debe elegir un item" value="" disabled="disabled">Elija una opción...</option>
<option value = "1">Clase 1</option>
<option value = "2">Clase 2</option>
<option value = "3">Clase 3</option>
<option value = "4">Clase 4</option>
<option value = "5">Clase 5</option>
</select>
</div>
<div class="ui submit red button">Registrarse</div>
<div class="ui error message"></div>
</div>
</form>
I have two databases. One where I keep the basic data and do the following:
$nombre = $_POST['nombre'];
if (isset($_POST['clase'])) {
$ul_list = json_encode($_POST['clase']);
} else {
$ul_list = json_encode(array("0"));
}
$conexion = new Conexion();
$stmt = $conexion -> prepare("INSERT INTO prueba (nombre, clase) VALUES (:nombre, :clase)");
$stmt->bindValue(":nombre", $nombre);
$stmt->bindValue(":clase", $ul_list);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$resultado = 1;
} else {
$resultado = null;
}
So everything is perfect: the data that I record is of the format:
[id] / [name] / [class]
1 MARIO PEREZ ["1", "3", "5"]
2 PEPE ROMERO ["1", "2", "5"]
3 JOSE PERDOMO ["1", "4"]
Now, I have another base where I have this structure: [id] / [name] / [class1] / [class2] / [class3] / [class4] / [class5]
in each "classN" field I want to save a boolean, yes or no ... and I do the following:
$clase1 = 0;
$clase2 = 0;
$clase3 = 0;
$clase4 = 0;
$clase5 = 0;
$array = json_decode($ul_list, true);
print_r ($array);
foreach ($array as $key=> $values) {
switch ($values) {
case 1:
$clase1 = 1;
case 2:
$clase2 = 1;
case 3:
$clase3 = 1;
case 4:
$clase4 = 1;
case 5:
$clase5 = 1;
}
}
$conexion = new Conexion();
$stmt = $conexion -> prepare("INSERT INTO prueba2 (nombre, clase1, clase2, clase3, clase4, clase5) VALUES (:nombre, :clase1, :clase2, :clase3, :clase4, :clase5)");
$stmt->bindValue(":nombre", $nombre);
$stmt->bindValue(":clase1", $clase1);
$stmt->bindValue(":clase2", $clase2);
$stmt->bindValue(":clase3", $clase3);
$stmt->bindValue(":clase4", $clase4);
$stmt->bindValue(":clase5", $clase5);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$resultado2 = 1;
} else {
$resultado2 = null;
}
It turns out that when I execute the code, all the fields leave them with the number 1 and it does not respect the values of the json
For example in the example: 1 MARIO PEREZ ["1", "3", "5"]
I should record
1 MARIO PEREZ 1 0 1 0 1
but he puts a number 1 in all the class fields ...
What am I doing wrong?