Checkebox with database data

Asked

Viewed 46 times

-2

I rephrased the Personal Question

I need to update vehicle, be presented all drivers who can drive the vehicle,( as well as in the registration table), but marking those who were chosen by the user at the time of registration.

Register the vehicle I can, but I’m not sure how to update. I’m not sure how to show selected and saved CHECBOX in the BD.

I tried to use the IF logic, comparing if the driver of the driver table is equal to the driver table id. If true, then the variable $status would receive the value of "checked", demonstrating as soon as that CHECBOX was marked at the time of registration by the user. That is, if IF is true.

But comparison logic is not working, I’m wrong. Remembering that all id are integer numbers.

Below is the code and images. Thanks qq help

I have three tables. tb_vehicles, tb_drivers, tb_vei_mot.

inserir a descrição da imagem aqui

**SELECTION OF VEHICLES ** <? php //include_once 'a_conexao.php'; include "IMPORTS/connected.inc";

                 $veiculos = "SELECT veiculo.id, veiculo.placa, vei_mot.motorista FROM veiculo
                 LEFT JOIN vei_mot
                 ON(vei_mot.veiculo=veiculo.id)
                 WHERE veiculo.id='1'" ;

                 $sel_veiculos = mysqli_query($conexao, $veiculos);
                  while($res_sel_veiculos = mysqli_fetch_assoc($sel_veiculos)){                         
                    echo $res_sel_veiculos['placa']."</br>";
                    echo $res_sel_veiculos['motorista']."</br>";
                }   
                
        ?>

**SELECTION OF DRIVERS **

 <?php
                 $motorista = "SELECT id AS id_motorista, nome FROM motorista";
                 $sel_motorista = mysqli_query($conexao, $motorista);
               ?>
               <td> 
                <?php
                  $estado;
                  while($res_sel_motoristas = mysqli_fetch_assoc($sel_motorista)){  
                    if(($res_sel_veiculos['motorista'])==1){
                        $estado="checked" ;                          
                    }else{
                        $estado="" ; 
                    }     
                    echo "<input type='checkbox' name='motorista[]' value=".$res_sel_motoristas['id_motorista']."  $estado/>".$res_sel_motoristas['nome']."</br>";
                }                   
               ?>                   
               </td>
               </tr>   
  • $select['driver'])=='1', this 1 is stored as what kind of given table? String, number, boolean... It is possible that $selection['driver'])=1 solve. Supplement the question with more information about the tables and data.

  • All ids are stored as numbers, type integer. And each id number represents a driver or a vehicle. NO BD, table driver, id 01 is the driver john, id 02 is the Roberto. Da msm forma in table vehicles, id 01, vehicle PCC0440, id 02 vehicle RRF5544.

  • I wanted to understand the correct logic to make the CHECBOX be marked if the driver item is equal to the id_driver table vehicles, where there are all drivers. Bring the data I can, but this simple logic of if id_driver is equal to 1, checked in chebox n worked.

  • If it is integer, the comparison is without quotation marks (quotation marks would be for string, for example) at number one: ($selection['driver'])==1 and in echo you have to use $status, not $situation. The $situation does nothing in this part of the code. Check the code more calmly and attentively, has more things to improve.

  • avoid using code images, put code text that formatted gets much better to view, remember that some people access from mobile and the images may get very small

1 answer

1

Basically, your if is wrong. It should be written like this:

if(($selecao['id'])=='1'){
    $estado="checked" ;  
 }else{
    $estado="" ;  
 }

There’s one more error I’ve seen in your echo. It’s calling the wrong variable to check or not. It should look like this:

echo "<input type='checkbox' name='motorista[]' value=".$id."  $estado/>".$nome."</br>";
  • 1

    I fix the code. Pd please review? Still n found the solution

Browser other questions tagged

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