SELECT and INSERT - Inserting only the first data

Asked

Viewed 82 times

0

I wanted to save the two data (from sector and from sector) but he is saving only the first in the two

Insertion script (html):

<div class="form-group">
    <label for="de_setor" class="w3-text-black">Do setor de:</label>
    <select class="form-control" required id="de_setor" name="de_setor">
        <option value=""disabled selected hidden>Selecione...</option>
        <?php
            while($linha = $pegadiv->fetch(PDO::FETCH_ASSOC)){
        ?>
            <option name="<?= $linha['setor_id'] ?>"> <?= $linha['setor'] ?> </option>
        <?php
            }
        ?>
    </select>


    <label for="para_setor" class="w3-text-black">Para o setor de:</label>
    <select class="form-control" required id="para_setor" name="para_setor">
        <option value=""disabled selected hidden>Selecione...</option>
        <?php
            while($linha2 = $pegadiv2->fetch(PDO::FETCH_ASSOC)){
        ?>
            <option name="<?= $linha2['setor_id'] ?>"> <?= $linha2['setor'] ?> </option>
        <?php
            }
        ?>
    </select>
</div>

Insertion script (php):

$acao  = (isset($_POST['acao'])) ? $_POST['acao'] : '';
$numero = $_POST['numero'];
$assunto = $_POST['assunto'];
$requerente = $_POST['requerente'];
$status = $_POST['status'];
$de_setor = (isset($_POST['de_setor']));
$para_setor = (isset($_POST['para_setor']));
$informe = $_POST['informe'];
$data = $_POST['data'];
$informante = (isset($_POST['informante']));
$processo_id = (isset($_POST['processo_id']));

 if ($acao == 'incluir'){
    $processos = "INSERT INTO processos(numero, assunto, requerente, status)VALUES(:numero, :assunto, :requerente, :status)";
    $stm = $conexao->prepare($processos);
    $stm->bindValue(':numero', $numero);
    $stm->bindValue(':assunto', $assunto);
    $stm->bindValue(':requerente', $requerente);
    $stm->bindValue(':status', $status);
    if($stm->execute()){
        
        $lastid = $conexao->lastInsertId();
        $informes = "INSERT INTO informes(de_setor, para_setor, informe, data, informante, processo_id)VALUES (:de_setor, :para_setor, :informe, :data, :informante, :processo_id)";
        $tsm = $conexao->prepare($informes);
        $tsm->bindValue(':de_setor', $de_setor);
        $tsm->bindValue(':para_setor', $para_setor);
        $tsm->bindValue(':informe', $informe);
        $tsm->bindValue(':data', $data);
        $tsm->bindValue(':informante', $informante);
        $tsm->bindValue(':processo_id', $lastid);
        if ($tsm->execute()){

BD:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • 1

    How are you saving the form? How do you recover the values of the fields in the Formulars? Try returning the "$para_sector". See if it’s coming right from the form

  • 1

    Ué? to saving with the insert script in php. all_processo.php. is in Pdo

  • 1

    $para_setor = (isset($_POST['para_setor'])); is returning right?

  • 1

    I believe so, after all the problem is not in the insertion process but in the select that pulls the </option> from the database. <label for="de_sector" class="W3-text-black">Sector of:</label> <select class="form-control" required id="de_sector" name="de_sector"> <option value=""disabled Selected Hidden>Select...</option> <? php while($line = $pegadiv->fetchAll(PDO::FETCH_ASSOC)){ ? > <option value="<?= $line['setor_id'] ? >""<?= $line['sector'] ? >"></option> <? php } ? > </select>

  • 1

    the problem is here :<option value="<?= $line['setor_id'] ? >""<?= $line['sector'] ? >"> this way he will only see the setor_id , because the other is outside the scope

  • 1

    or you concatenate or you create another attribute that receives it

  • 1

    how I would concatenate that line?

  • if you can help me, I’d appreciate it! thanks in advance.

Show 3 more comments

1 answer

0


The mistake is in instead of name must use value in both <select>:

<option name="<?= $linha2['setor_id'] ?>"> <?= $linha2['setor'] ?> </option>

But they may be:

1st

<option value="<?php echo $linha2['setor_id']; ?>"> <?php echo $linha2['setor']; ?> </option>

2nd

<select class="form-control" required id="de_setor" name="de_setor"> 
    <option value=""disabled selected hidden>Selecione...</option> 
    <?php while($linha = $pegadiv->fetchAll(PDO::FETCH_ASSOC)){ 
        echo '<option value="'.$linha['setor_id'].'">'.$linha['setor'].'</option>';
    }?> 
</select> 
  • 1

    now giving this error: Notice: Undefined index: sector in C: xampp htdocs test registrarprocesso.php on line 122

  • edited the answer

  • 1

    now it is not pulling the data. I open the select, in the same form, and only appears a blank data.

  • None of the two options I gave? 1tbm ta edited

  • 1

    a 1° the Undefined index appears, already at 2° it is the blank

  • please, if you can help me I’d be grateful!

Show 1 more comment

Browser other questions tagged

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