3
How do I make multiple records with checkbox?
I need to make multiple records according to my appointment on each checkbox and include in the column id_item the same ID that comes from the variable  $proximoID in all records.
I’m trying this, only it’s not working:
<form action="#" method="POST" enctype="multipart/form-data">
Flights on: <br/>
<input type="checkbox" name="categoria[]" value="1">segunda<br>
<input type="checkbox" name="categoria[]" value="2">Sunday<br>
<input type="checkbox" name="categoria[]" value="3">Monday<br>
<input type="checkbox" name="categoria[]" value="4">Tuesday <br>
<input type="checkbox" name="categoria[]" value="5">Wednesday<br>
<input type="checkbox" name="categoria[]" value="6">Thursday <br>
<input type="checkbox" name="categoria[]" value="7">Friday<br>
<input type="checkbox" name="categoria[]" value="8">Saturday <br>
<input type="submit" name="insert" value="submit">
</form>
  if(isset($_POST['insert']))
  {
    $checkBox = $_POST['categoria'];
                    for ($i=0; $i<sizeof($checkBox); $i++) {
                    $cadastrarItem = $DB->prepare("INSERT INTO teste (id_item, id_category) VALUES ($proximoID, '" . $checkBox[$i] . "')");
          }
            $cadastrarItem->execute();
                    if($cadastrarItem->rowCount() >= 0)
                    {
                        echo 'sucesso';
                    }
                    else
                    {
                        echo 'erro';
                    }
  }
/
/ ESSE CÓDIGO É PARA PEGAR O ID DO ITEM, POR EXEMPLO A VARIAVEL $proximoID VAI DAR UM NUMERO, EU PRECISO QUE ESSE NUMERO ESTEJA NOS MULTIPLOS REGISTRO E ALTERE APENAS O id DO $checkBox
      try {
      $sql = "SHOW TABLE STATUS LIKE 'teste' ";
      $stmt = $DB->prepare($sql);
      $stmt->execute();
      $resultado = $stmt->fetch();
      $proximoID = $resultado['Auto_increment'];  // a chave esta aqui
       } catch (Exception $ex) {
       echo $ex->getMessage();
      }
      echo $proximoID;
@Edit
$checkBox = array_filter($_POST['categoria'], 'is_int');
// Segurança: apenas haverá números inteiros, dessa forma se houver: (1,2,3,biscoito,5,10,lasanha) irá ser: (1,2,3,5,10)
$sqlParcial = '';
for ($i=0; $i < count($checkBox); $i++) {
$sqlParcial .= '("'. $checkBox[$i] .'", (SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = "teste" AND table_schema = DATABASE()) -'. $i .'),';
}
$cadastrarItem = $DB->prepare("INSERT INTO teste (id_category, id_item) VALUES ". $sqlParcial."");
$cadastrarItem->execute();
if($cadastrarItem->rowCount() >= 0)
{
    echo 'sucesso';
}
  else
{
  echo 'erro';
}
You want to insert repeated Ids into a column of auto increment?
– Bacco
@Bacco No, I want to insert the repeated ids only in the 'id_item' column'
– William Alvares
Perhaps it would be better to abandon the code to take the ID, and take the first INSERT then. Theoretically, the first item id_item would correspond to its auto_increment. Then the first one would have to give an UPDATE, but it is safer than picking up the item before;
– Bacco
@Bacco I did not put the column ID in the Insert auto increment..
– William Alvares
I got that, but you’re taking it as the initial ID, and copying it on the id_item, right? Another thing, you can concatenate the VALUES and make an Insert only. (repeating the pairs within VALUES( ) )
– Bacco
@Bacco No, like I have 500 records in a table, this other 'test' table that is in the code, will separate the 500 records in a given category
– William Alvares
Ah good. It is that in the two codes the table is "test", so I found strange.
– Bacco
Nesssthe table 'test' has 3 column, id, id_item, id_category, id is auto increment, id_item is id auto increment of the table otura posts, and id_category is the value of the HTML that is in the code
– William Alvares
The
$proximoIDis equal to$idItem?– Inkeliz
@Inkeliz Yes, I forgot to erase
– William Alvares
@Inkeliz Ready, edited the post
– William Alvares
Something is inserted in the table
teste?– Inkeliz
@Inkeliz Type, when I put only 1 column, it worked, then I put the 2 column and only sends 1 record being q I selected 3 checkbox
– William Alvares