Well, what I do is send the value of a specific checkbox to a database, but it is saved in another field that I indicate. Could you help me with the logic, since I think that is where the error is: I "draw" this in a while loop to display all the results of a mysql and php query
<form role="form" id="quickForm" method="post" action="condiciones_productos.php">
<div class="card-body"><input type="submit" name="cambiar" class="btn btn-primary" value="cambiar" >
<table id="example1" class="table ">
<thead>
<tr>
<th>Nuevo #Codigo</th>
<th>Producto</th>
<th>Color</th>
<th>Flujo Mayor</th>
<th>Flujo Menor</th>
</tr>
</thead>
<tbody>
<?php
include('my_function.php');
$currency = getCurrency();
$sql = "SELECT * FROM plantilla ORDER BY id ASC";
$result = mysqli_query($con, $sql);
$i = 1;
$n=$codigo;
$min=1000;
$max=0;
while ($row = mysqli_fetch_array($result)) {
$n=$n+1;
$temp=$row['id'];
if ($row['flujo_mayor'] === 'true') {
$checked = 'checked="checked" ';
} else {
$checked = ' ';
}
if ($row['flujo_menor'] === 'true') {
$checked0 = 'checked="checked" ';
} else {
$checked0 = ' ';
}
if ($min>=$temp)
{
$min=$temp;
}
if($max<=$temp)
{
$max=$temp;
}
echo "<tr>";
echo "<td><input type='hidden' name='codigo_".$row['id']."' class='form-control' id='exampleInputStock' value=" . $n . ">" . $n . "</td>";
echo "<input type='hidden' name='id[]' class='form-control' id='exampleInputStock' value=" . $temp . ">";
echo "<td><input type='' name='nombre_producto[]' class='form-control' id='exampleInputStock' value='" . $row['nombre_producto']." ". $modelo."'>". "</td>";
echo "<td><input type='' name='nombre_color[]' class='form-control' id='exampleInputStock' value='". $row['color_producto']."'>". "</td>";
echo "<td><input type='checkbox'name='flujo_mayor[]' class='form-control' id='exampleInputStock' value='true' $checked></td>";
echo "<td><input type='checkbox'name='flujo_menor[]' class='form-control' id='exampleInputStock' value='true' $checked0></td>";
echo "</tr> ";
}
echo " </tbody>";
echo " </table>";
?>
</form>
my update with php is something like this
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$Update_producto = $_POST['nombre_producto'];
$Update_mayor = $_POST['flujo_mayor'];
$Update_menor = $_POST['flujo_menor'];
$Update_color = $_POST['nombre_color'];
$id = $_POST['id'];
// Haces un ciclo para recorrer uno de los campos y usar el mismo índice para los otros:
foreach($Update_menor as $pro => $valor) {
// Los elementos de arreglo deben encerrarse entre llaves
$actualizar = "UPDATE plantilla SET flujo_mayor='$Update_mayor[$pro]',flujo_menor='$Update_menor[$pro]' WHERE nombre_producto='$Update_producto[$pro]' and color_producto='$Update_color[$pro]'";
// Aquí ejecutas la consulta para insertar este producto
$resultado_productos = $con->query($actualizar);
}
}
all the other data is saved in its place and correct order only the checkbox part is the one that is sent at the beginning, for example mark the product 50 as a major flow this should send the value of the checkbox to field 50 of the database but instead sends it to the first field
Before we start, avoid those
id="exampleInputStock"
, because you're echoing and they must be unique , if it's for styling then use classes.When starting the loop, you are saved Product ID with
$temp=$row['id'];
and can use it as an index for all fields.Since you are enclosing strings in double quotes, PHP can interpret the variables and only array elements need to be enclosed in braces to avoid concatenation.
With this, all the fields will have the indexes associated with the product ID and you only have to check if it
checkbox
was marked:In the loop you are looping through only the
checkbox
minor_flow ones that were marked, so no further validation is necessary.Finally, your script is vulnerable to SQL attacks and prone to errors when quotes are included in values. I recommend you switch to prepared statements .