What is the best way to update database through checkbox in PHP / MySQL? [duplicate]

0

Good afternoon!

On this site the user informs the CNPJ, STATUS and TRAVEL and search. When searching it shows in the result-table below the result of a SELECT of the DTC table of the database, according to the data entered.

It also has the Changes part, where the user informs other data from the same table in the database. When you click Refresh it should UPDATE in the table with the data entered. But only for the lines that are selected by the checkbox. What is the best way to do that since the checkbox is in a table and should it do a check and change row by row of those that are checked? Would you be using vector?

Hereisthecodeforthepage:

<!-- SMARV Informática MEI (91)98156-5857-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>VERIFICACAO DE CARGA</title>
  <style type="text/css">
    <!-- .style1 {
      color: #FF0000;
      font-size: x-small;
    }
    
    .style3 {
      color: #0000FF;
      font-size: x-small;
    }
    
    -->
  </style>
  <!--
						########## • Créditos • #############
						#									#
						#	Autor: Wesley C. Nascimento		#
						#	Email: [email protected]	#
						#									#
						#	Nome do script:FormularioCliente#
						#	Bonus: validaCampo				#
						#									#
						#	Favor não retire os créditos	#
						#	Duvidas por mandem email.		#
						#									#
						#####################################

-->
  <style>
    table {
      border-collapse: collapse;
      width: 50%;
      border: 1px solid black;
    }
    
    th,
    td {
      text-align: left;
      padding: 8px;
    }
    
    tr:nth-child(even) {
      background-color: #f2f2f2
    }
    
    th {
      background-color: #ba0000;
      color: white;
    }
  </style>
</head>

<body bgcolor="#FFFFF0">
  <?php
$stat=null;
if (isset($_POST["status"])){
	$stat = $_POST ["status"];
}
?>
    <style>
      table {
        float: left;
      }
    </style>
    <form id="localizacao" name="localizacao" method="post" action="rastreamento.php" onsubmit="return true;">
      <table width="100%" border="0">
        <tr>
          <div align="center"><img src=img/logo.jpg></div>
        </tr>
      </table>
      <table width="50%" border="0">
        <tr>
          <th colspan="5" align="center" valign="top">
            <h2>Localizacao de Carga</h2>
          </th>
        </tr>
        <tr>
          <td>CNPJ:</td>
          <td width="835"><input name="CNPJ" type="text" id="CNPJ" size="20" maxlength="14" />
            <span class="style1">*</span> <span class="style3">somente n&uacute;meros</span></td>
        </tr>
        <tr>
          <td width="156">Selecione o STATUS:</td>
          <td>
            <select name="status" id="status">
              <option value="0" <?=($stat=='0' )? 'selected': ''?>>Recebido</option>
              <option value="1" <?=($stat=='1' )? 'selected': ''?>>Em trânsito</option>
              <option value="2" <?=($stat=='2' )? 'selected': ''?>>Encerrado</option>
            </select>
          </td>
        </tr>
        <tr>
          <td>
            VIAGEM:
          </td>
          <td width="835"><input name="VIAGEM" type="text" id="VIAGEM" size="6" maxlength="6" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <p>
              <input name="pesquisar" type="submit" id="pesquisar" value="Pesquisar" />
              <br />
              <input name="limpar" type="reset" id="limpar" value="Limpar!" />
              <br />
            </p>
            <br></br>
            <br></br>
            <br></br>
            <br></br>
            <p></p>
          </td>
        </tr>
      </table>
      <?php
    //Pega o valor do CNPJ e do status escolhido na página anterior
    $CNPJ=null;
	$STATUS=null;
	$VIAGEM=null;
	if (isset($_POST["CNPJ"])){
		$CNPJ = $_POST ["CNPJ"];
	}
	if (isset($_POST["status"])){
		$STATUS = $_POST ["status"];
	}	
    if (isset($_POST["VIAGEM"])){
		$VIAGEM = $_POST ["VIAGEM"];
	}	  	
	?>
    </form>

    <form id="alteracoes" name="alteracoes" method="post" action="AlteraStatus.php" onsubmit="return true;">
      <table width="50%" border="0">
        <tr>
          <th colspan="5" align="center" valign="top">
            <h2>Alteracoes</h2>
          </th>
        </tr>
        <tr>
          <td width="50">Viagem:</td>
          <td width="835"><input name="VIAGEM" type="text" id="VIAGEM" size="20" maxlength="14" value="" /></td>
        </tr>
        <tr>
          <td width="165">Novo STATUS:</td>
          <td>
            <select name="status" id="status">
              <option value="0">Recebido</option>
              <option value="1">Em trânsito</option>
              <option value="2">Encerrado</option>
            </select>
          </td>

        </tr>
        <tr>
          <td width="50">Embarcação:</td>
          <td width="835"><input name="EMBARC" type="text" id="EMBARC" size="20" maxlength="10" value="" /></td>
        </tr>
        <tr>
          <td width="50">Data Saída:</td>
          <td width="835"><input name="DATSAI" type="text" id="DATSAI" size="20" maxlength="10" value="" /><span class="style1"> Formato DDMMAAA</span></td>
        </tr>
        <tr>
          <td width="50">Hora Saída:</td>
          <td width="835"><input name="HORSAI" type="text" id="HORSAI" size="20" maxlength="10" value="" /><span class="style1"> Formato HHMM</span></td>
        </tr>
        <tr>
          <td width="50">Data Chegada:</td>
          <td width="835"><input name="DATCHE" type="text" id="DATCHE" size="20" maxlength="10" value="" /><span class="style1"> Formato DDMMAAA</span></td>
        </tr>
        <tr>
          <td width="50">Hora Chegada:</td>
          <td width="835"><input name="HORCHE" type="text" id="HORCHE" size="20" maxlength="10" value="" /><span class="style1"> Formato HHMM</span></td>
        </tr>
        <tr>
          <td width="50">Origem:</td>
          <td width="835"><input name="ORIG" type="text" id="ORIG" size="20" maxlength="10" value="" /></td>
        </tr>
        <tr>
          <td width="50">Destino:</td>
          <td width="835"><input name="DEST" type="text" id="DEST" size="20" maxlength="10" value="" /></td>
        </tr>
        <tr>
          <td colspan="2">
            <p>
              <input name="atualizar" type="submit" id="atualizar" value="Atualizar" />
              <br />
            </p>
          </td>
        </tr>
      </table>
    </form>
    <!-- Lista cada documento de acordo com o CNPJ, o STATUS e a Viagem-->
    <?php 		
	echo '<table width="100%" border="1">';
	echo '<thead><tr>';
	echo '<th><input type="checkbox" name="checkTodos" id="checkTodos"></th>';
	echo '<th align="center">CNPJ_PAG</th>';
	echo '<th align="center">Data_REC</th>';
	echo '<th align="center">CNPJ_FOR</th>';
	echo '<th align="center">Tipo_Doc</th>';
	echo '<th>N&ordm;_Doc</th>';
	echo '<th>Fornec</th>';
	echo '<th>Quant. Vol</th>';
	echo '<th>Processo</th>';	
	echo '<th>STATUS</th>';
	echo '<th>Origem</th>';
	echo '<th>Viagem</th>';
	echo '<th>Destino</th>';
	echo '<th>Embarcação</th>';
	echo '<th>Data_Saida</th>';
	echo '<th>Hora_Saida</th>';
	echo '<th>Data_Cheg</th>';
	echo '<th>Hora_Chegada</th>';
	echo '</tr></thead>';
				
		//realiza a conexão com o banco de dados
		include("conexao.php");
		//Faz a consulta de acordo com o CNPJ, o status	e a viagem
		$query = "SELECT * FROM DTC WHERE ";
		if ($CNPJ != null){
			$query = $query."DTC_CGCPAG='$CNPJ' ";
		}
		if ($STATUS != null){
			if ($CNPJ != null){
				
				$query = $query."AND ";
			}
			$query = $query."DTC_STATUS='$STATUS' ";
		}
		if ($VIAGEM != null){
			if ($CNPJ != null OR $STATUS != null){
				$query = $query."AND ";
			}
			$query = $query."DTC_NUMVGA='$VIAGEM' ";
		}
		if ($query == "SELECT * FROM DTC WHERE "){
			$query = "SELECT * FROM DTC WHERE DTC_CGCPAG=0";
		}	
	
		$sql_dtc = mysqli_query($cx, $query) or die(mysqli_error($cx));	
	
		echo '<tbody>';
		$contador=0;
		while($aux = mysqli_fetch_assoc($sql_dtc)) { 
			echo '<tr>';
			$contador=$contador+1;
			echo '<td><input type="checkbox" name="'.$contador.'" id=""></td>';
			echo $contador;
			echo '<td>'.$aux["DTC_CGCPAG"].'</td>';
			echo '<td>'.$aux["DTC_DATREC"].'</td>';
			echo '<td>'.$aux["DTC_CGCFOR"].'</td>';
			echo '<td>'.$aux["DTC_TIPDOC"].'</td>';
			echo '<td>'.$aux["DTC_NUMNFC"].'</td>';
			echo '<td>'.$aux["DTC_FORNEC"].'</td>';
			echo '<td>'.$aux["DTC_QTDVOL"].'</td>';
			echo '<td>'.$aux["DTC_PROCES"].'</td>';
			if ($aux["DTC_STATUS"]==0){
				echo '<td>'.'Recebido'.'</td>';
			}
			elseif ($aux["DTC_STATUS"]==1){
				echo '<td>'.'Em trânsito'.'</td>';
			}
			else{
				echo '<td>'.'Encerrado'.'</td>';
			}
			echo '<td>'.$aux["DTC_CDRORI"].'</td>';
			echo '<td>'.$aux["DTC_NUMVGA"].'</td>';
			echo '<td>'.$aux["DTC_CDRDES"].'</td>';
			echo '<td>'.$aux["DTC_CODVEI"].'</td>';
			echo '<td>'.$aux["DTC_DATSAI"].'</td>';
			echo '<td>'.$aux["DTC_HORSAI"].'</td>';
			echo '<td>'.$aux["DTC_DATCHE"].'</td>';
			echo '<td>'.$aux["DTC_HORCHE"].'</td>';
			echo '</tr>';
		}
		echo '</tbody></table>';
		
	mysqli_close($cx);
	
	// Altera o status dos documentos
	function AlteraStatus($VIAGEM,$STATUS,$EMBARC,$DATSAI,$HORSAI,$DATCHE,$HORCHE,$ORIGEM,$DESTINO){
		
	return true;
	}
	?>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scripttype="text/javascript">
      function validaCampoAltera() {
        if (document.alteracoes.VIAGEM.value == "") {
          alert("O Campo VIAGEM é obrigatório!");
          return false;
        } else
          return true;
      }
      // Fim do JavaScript que validará os campos obrigatórios!
      <!-- Script que marca todos os checkboxes -->
      $("#checkTodos").click(function() {
        $('input:checkbox').not(this).prop('checked', this.checked);
      });
    </script>
</body>

</html>
<!-- SMARV Informática MEI (91)98156-5857-->

Thanks to all of you right away.

    
asked by anonymous 19.07.2018 / 00:06

0 answers