0
I am not being able to update the fields that are only selected by CHECKBOX, which I was able to develop right below:
BD.SQL
CREATE DATABASE IF NOT EXISTS `seq_acessos` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `seq_acessos`;
CREATE TABLE `teste2` (
`id` int(11) NOT NULL,
`teste1` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`teste2` varchar(20) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
INSERT INTO `teste2` (`id`, `teste1`, `teste2`) VALUES
(5, '02', '22'),
(6, '03', '33'),
(7, '04', '44');
ALTER TABLE `teste2`
ADD PRIMARY KEY (`id`);
ALTER TABLE `teste2`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
INDEX.PHP
error_reporting ( E_ALL );
ini_set( 'display_errors', TRUE );
$host = "localhost";
$usuario = "root";
$senha = "";
$bd = "teste";
$mysqli = new mysqli($host, $usuario, $senha, $bd);
if($mysqli->connect_errno){
echo "Falha na Conexão: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
}
$sql_code = "SELECT * FROM teste2";
$sql_query = $mysqli->query($sql_code) or die($mysqli->error);
?>
<br>
<br>
<form action="" method="post">
<?php while($linha = $sql_query->fetch_assoc()){ ?>
<input type="checkbox" name="teste[cb][]" value="<?php echo $linha['id']; ?>">
<input type="text" name="teste[teste1][]" value="<?php echo $linha['teste1']; ?>">
<input type="text" name="teste[teste2][]" value="<?php echo $linha['teste2']; ?>">
<?php } ?>
<br><br>
<input type="submit" value="OK">
</form>
<?php
if(isset($_POST)){
$field = $_POST;
$count = count($field[teste]);
for($i = 0; $i < $count; $i++){
$up .= "UPDATE table SET teste1 = {$field['teste']['teste1'][$i]}, teste2 = {$field['teste']['teste2'][$i]} WHERE id = $linha[id] \n";
}
echo "<pre>";
print_r($up);
print_r($_POST);
}
Lucas, thanks for your attention, but I couldn’t test your idea, even adding the FOR closure, the $query is not being displayed.
– Cristiano Iglesias
@Cristianoiglesias, try again the adjustments.
– Lucas Monteiro
Just for the record, it turned out exactly the way I wanted it, thank you!
– Cristiano Iglesias