How to perform update on records marked with checkbox in a while

0

I'm not able to UPDATE the fields that are only selected by CHECKBOX, which I was able to develop 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);    

}
    
asked by anonymous 07.04.2016 / 23:50

2 answers

1
 <?php 
    $linha = [];

    $linha[1] = array("id" => 1, 'field' => "valor", 'field2' => "valor2");
    $linha[2] = array("id" => 2, 'field' => "valor", 'field2' => "valor2");
    $linha[3] = array("id" => 3, 'field' => "valor", 'field2' => "valor2");
    $linha[4] = array("id" => 4, 'field' => "valor", 'field2' => "valor2");
    $linha[5] = array("id" => 5, 'field' => "valor", 'field2' => "valor2");

    echo '<form action="" method="post">';
    echo '<ul>';
    for ($i=1; $i <= count($linha); $i++) { 

        echo '<li><input type="checkbox" name="teste[ex'.$i.'][id]"      value="'.$linha[$i]["id"].'"></li>
        <li><input type="text"  name="teste[ex'.$i.'][field]" value="'.$linha[$i]["field"].'"></li>
        <li><input type="text"  name="teste[ex'.$i.'][field2]" value="'.$linha[$i]["field2"].'"></li>';
    }
    echo '</ul><br><br>
       <input type="submit" value="OK">
    </form>';

    if (isset($_POST['teste'])) {
       $data = $_POST['teste']; 
       if ($data != null) {
            foreach ($data as $key => $value) {
                if (isset($value['id']) && !empty($value['id'])) {
                    $query = "UPDATE table SET field =".$value['field'].",field2 =".$value['field2']." WHERE id= ".$value['id'];
                    $query = rtrim($query, ", ");
                    echo "<pre>$query";
                }
            }
        }
    }
    ?>
    
08.04.2016 / 02:21
0

So I understand you want to get the values that are in inputs of type text according to the checked checkbox. For this you have to see if the corresponding checkbox has been marked and get the value of it that will be used as reference to get the% of text . Here is an example:

<?php
  if(isset($_POST["submit"])){

    $checkbox = $_POST["teste"]['cb'];


    if(is_array($checkbox) && !empty($checkbox)){

        $up = '';

        foreach($checkbox as $key=>$value){

            $teste1 = $_POST['teste']["teste1"][$key];
            $teste2 = $_POST['teste']["teste2"][$key];

            $up .= "UPDATE table SET teste1 = '".$teste1."', teste2 = '".$teste2."' WHERE id = '".$value."' <br />";
        }

        echo $up;
    }
  }
?>

<?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);
?>
<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'];?>">
    <br />
    <?php } ?>

    <input type="submit" name="submit">
</form>
    
08.04.2016 / 00:13