How to update records marked with checkbox at a while

Asked

Viewed 336 times

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);    

}

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";
                }
            }
        }
    }
    ?>
  • Lucas, thanks for your attention, but I couldn’t test your idea, even adding the FOR closure, the $query is not being displayed.

  • @Cristianoiglesias, try again the adjustments.

  • Just for the record, it turned out exactly the way I wanted it, thank you!

0

From what I understand you want to take the values that are in inputs of the kind text according to the checkbox marked. For this you have to see if the corresponding checkbox has been marked and take the value of it that will be used as reference to take the text. Follow 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>
  • Yes, I need to change only the lines marked with the checkbox, in the example above the BD has 3 records: perfect, but not identifying the correct values of imputs. I performed a test by changing the 1 and 3 record and the result was: UPDATE table SET teste1 = '02', teste2 = '22' WHERE id = '2' UPDATE table SET teste1 = '03', teste2 = '33' WHERE id = '4'

  • I will try to detail a little more: If I check the three checkboxes UPDATES is correctly created: UPDATE table SET teste1 = '02', teste2 = '22' WHERE id = '2' UPDATE table SET teste1 = '03', teste2 = '33' WHERE id = '3' UPDATE table SET teste1 = '04', teste2 = '44' WHERE id = '4' But as I mentioned earlier, if I make only the changes of 1 and 3 fields, the result is this: UPDATE table SET teste1 = '02', teste2 = '22' WHERE id = '2' UPDATE table SET teste1 = '03', teste2 = '33' WHERE id = '4' UPDATE is created with incorrect values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.