Save data to a table in MYsql database using php

1

How to save data from the top column and line example I have description (row) + date (column) + value (column) = same row column.

<form name="frmitementrada" id="frmitementrada">
<table width="89%"  style="font-size:12px;" id="tabela1">
		<tr id="1" style="background-color: #4171a1; color:#ffffff; font-weight: bold; font-family:Arial, Helvetica, sans-serif;">
		  <td width="20%">DIA</td>
		  <td ><?php  $d1 = date('Y-m-d') ;
	
$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
echo $data->format('l');
?></td>
			<td width="9%" style="display: none; text-align: center;">TERÇA</td>
			<td style="display: none; text-align: center;">QUARTA</td>
			<td style="display: none; text-align: center;">QUINTA</td>
			<td style="display: none; text-align: center;">SEXTA</td>
			<td style="display: none; text-align: center;">SABADO</td>
			<td style="display: none; text-align: center;">DOMINGO</td>
			<td width="10%" style="display: none; text-align: center;">SEGUNDA</td>
            <td width="9%" rowspan="2">TOTAL PREIODO</td>
		</tr>
        <tr id="1" style="background-color: #4171a1; color:#ffffff; font-weight: bold; font-family:Arial, Helvetica, sans-serif;
	font-size:12px;">
          <td width="20%" >DATA</td>
          <td><?php
		 echo $d1 = date('d-m-Y', strtotime("+5 days")) ;
	
//$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
//echo $data->format('l');
          
          ?>
          </td>
			<td width="9%" style="display: none; text-align: center;"><?php
		 echo $d2 = date('d-m-Y', strtotime("+5 days")) ;
	
//$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
//echo $data->format('l');
          
          ?></td>
			<td width="8%" style="display: none; text-align: center;"><?php
		 echo $d3 = date('d-m-Y', strtotime("+5 days")) ;
	
//$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
//echo $data->format('l');
          
          ?></td>
			<td width="8%" style="display: none; text-align: center;"><?php
		 echo $d4 = date('d-m-Y', strtotime("+5 days")) ;
	
//$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
//echo $data->format('l');
          
          ?></td>
            <td width="8%" style="display: none; text-align: center;"><?php
		 echo $d5 = date('d-m-Y', strtotime("+5 days")) ;
	
//$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
//echo $data->format('l');
          
          ?></td>
            <td width="8%" style="display: none; text-align: center;"><?php
		 echo $d6 = date('d-m-Y', strtotime("+5 days")) ;
	
//$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
//echo $data->format('l');
          
          ?></td>
            <td width="10%" style="display: none; text-align: center;"><?php
		 echo $d7 = date('d-m-Y', strtotime("+5 days")) ;
	
//$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
//echo $data->format('l');
          
          ?></td>
            <td width="10%" style="display: none; text-align: center;"><?php
		 echo $d8 = date('d-m-Y', strtotime("+5 days")) ;
	
//$data = new DateTime($d1, new DateTimeZone('Europe/Lisbon'));
//echo $data->format('l');
          
          ?></td>
        </tr>
		<tr id="2" bgcolor="#dee0e1"  >
		  <td height="31"><label> <input type="text" name="" value="Exemplo almoço" id="descricao1" class="txt"/>
</label></td>
          <!-- Linha A-->
		  <td ><INPUT id="a1" class="txt" TYPE="text" SIZE="3" NAME="a1" onkeyup="multiplicala(), multiplicatotal()" value="0">
         </td>
		  <td style="display: none;"><INPUT id="a2" class="txt" TYPE="text" onkeyup="multiplicala(), multiplicatotal()" SIZE="3" NAME="a2"  value="0" ></td>
			<td style="display: none;"><INPUT id="a3" class="txt" TYPE="text" onkeyup="multiplicala(), multiplicatotal()" SIZE="3" NAME="a3"  value="0" ></td>
			<td style="display: none;"><INPUT id="a4" class="txt" TYPE="text" onkeyup="multiplicala(), multiplicatotal()" SIZE="3" NAME="a4"  value="0" ></td>
            <td width="8%" style="display: none;"><INPUT id="a5" class="txt" TYPE="text" onkeyup="multiplicala(), multiplicatotal()" SIZE="3" NAME="a5"  value="0" ></td>
            <td width="8%" style="display: none;"><INPUT id="a6" class="txt" TYPE="text" onkeyup="multiplicala(), multiplicatotal()" SIZE="3" NAME="a6"  value="0" ></td>
            <td width="10%" style="display: none;"><INPUT id="a7" class="txt" TYPE="text" onkeyup="multiplicala(), multiplicatotal()" SIZE="3" NAME="a7"  value="0" ></td>
            <td width="10%" style="display: none;"><INPUT id="a8" class="txt" TYPE="text" onkeyup="multiplicala(), multiplicatotal()" SIZE="3" NAME="a8"  value="0" ></td>
            <!-- PArte aparece o calculo da linha la-->
            <td align="right" ><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total_la" value="0" disabled></td>
		</tr>
		<tr id="3" style="display: none;" >
        <!-- Linha B-->
		  <td><label> <input type="text" name="" value="Descrição" id="" class="txt"/>
</label></td>
		  <td><INPUT id="b1" class="txt" TYPE="text" SIZE="3" NAME="b1" onkeyup="multiplicalb(), multiplicatotal()" value="0"></td>
		  <td style="display: none;"><INPUT id="b2" class="txt" TYPE="text" SIZE="3" NAME="b2" onkeyup="multiplicalb(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="b3" class="txt" TYPE="text" SIZE="3" NAME="b3" onkeyup="multiplicalb(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="b4" class="txt" TYPE="text" SIZE="3" NAME="b4" onkeyup="multiplicalb(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="b5" class="txt" TYPE="text" SIZE="3" NAME="b5" onkeyup="multiplicalb(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="b6" class="txt" TYPE="text" SIZE="3" NAME="b6" onkeyup="multiplicalb(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="b7" class="txt" TYPE="text" SIZE="3" NAME="b7" onkeyup="multiplicalb(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="b8" class="txt" TYPE="text" SIZE="3" NAME="b8" onkeyup="multiplicalb(), multiplicatotal()" value="0"></td>
             <td align="right" ><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total_lb" value="0" disabled></td>

		</tr>
		<tr id="4" bgcolor="#dee0e1" style="display: none;" >
        <!-- Linha C-->
		  <td><label> <input type="text" name="" value="Descrição" id="" class="txt"/>
</label></td>
		  <td><INPUT id="c1" class="txt" TYPE="text" SIZE="3" NAME="c1" onkeyup="multiplicalc(), multiplicatotal()" value="0"></td>
		  <td style="display: none;"><INPUT id="c2" class="txt" TYPE="text" SIZE="3" NAME="c2" onkeyup="multiplicalc(), multiplicatotal()" value="0"></td>
		<td style="display: none;"><INPUT id="c3" class="txt" TYPE="text" SIZE="3" NAME="c3" onkeyup="multiplicalc(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="c4" class="txt" TYPE="text" SIZE="3" NAME="c4" onkeyup="multiplicalc(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="c5" class="txt" TYPE="text" SIZE="3" NAME="c5" onkeyup="multiplicalc(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="c6" class="txt" TYPE="text" SIZE="3" NAME="c6" onkeyup="multiplicalc() , multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="c7" class="txt" TYPE="text" SIZE="3" NAME="c7" onkeyup="multiplicalc() , multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="c8" class="txt" TYPE="text" SIZE="3" NAME="c8" onkeyup="multiplicalc(), multiplicatotal()" value="0"></td>
             <td align="right" ><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total_lc" value="0" disabled></td>
		</tr>
        <tr id="5" style="display: none;" >
        <!-- Linha D-->
          <td><label> <input type="text" name="" value="Descrição" id="" class="txt"/>
</label></td>
          <td><INPUT id="d1" class="txt" TYPE="text" SIZE="3" NAME="d1" onkeyup="multiplicald(), multiplicatotal()" value="0"></td>
		  <td style="display: none;"><INPUT id="d2" class="txt" TYPE="text" SIZE="3" NAME="d2" onkeyup="multiplicald(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="d3" class="txt" TYPE="text" SIZE="3" NAME="d3" onkeyup="multiplicald(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="d4" class="txt" TYPE="text" SIZE="3" NAME="d4" onkeyup="multiplicald(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="d5" class="txt" TYPE="text" SIZE="3" NAME="d5" onkeyup="multiplicald(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="d6" class="txt" TYPE="text" SIZE="3" NAME="d6" onkeyup="multiplicald(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="d7" class="txt" TYPE="text" SIZE="3" NAME="d7" onkeyup="multiplicald(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="d8" class="txt" TYPE="text" SIZE="3" NAME="d8" onkeyup="multiplicald(), multiplicatotal()" value="0"></td>
             <td align="right" ><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total_ld" value="0" disabled></td>
		</tr>
        <tr id="6" bgcolor="#dee0e1"  style="display: none;" >
        <!-- Linha E-->
          <td><label> <input type="text" name="" value="Descrição" id="" class="txt"/>
</label></td>
          <td><INPUT id="e1" class="txt" TYPE="text" SIZE="3" NAME="e1" onkeyup="multiplicale(), multiplicatotal()" value="0"></td>
		  <td style="display: none;"><INPUT id="e2" class="txt" TYPE="text" SIZE="3" NAME="e2" onkeyup="multiplicale(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="e3" class="txt" TYPE="text" SIZE="3" NAME="e3" onkeyup="multiplicale(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="e4" class="txt" TYPE="text" SIZE="3" NAME="e4" onkeyup="multiplicale(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="e5" class="txt" TYPE="text" SIZE="3" NAME="e5" onkeyup="multiplicale(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="e6" class="txt" TYPE="text" SIZE="3" NAME="e6" onkeyup="multiplicale(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="e7" class="txt" TYPE="text" SIZE="3" NAME="e7" onkeyup="multiplicale(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="e8" class="txt" TYPE="text" SIZE="3" NAME="e8" onkeyup="multiplicale(), multiplicatotal()" value="0"></td>
             <td align="right" ><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total_le" value="0" disabled></td>
		</tr>
        <tr id="7" style="display: none;" >
        <!-- Linha F-->
          <td><label> <input type="text" name="" value="Descrição" id="" class="txt"/>
</label></td>
          <td><INPUT id="f1" class="txt" TYPE="text" SIZE="3" NAME="f1" onkeyup="multiplicalf(), multiplicatotal()" value="0"></td>
		  <td style="display: none;"><INPUT id="f2" class="txt" TYPE="text" SIZE="3" NAME="f2" onkeyup="multiplicalf(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="f3" class="txt" TYPE="text" SIZE="3" NAME="f3" onkeyup="multiplicalf(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="f4" class="txt" TYPE="text" SIZE="3" NAME="f4" onkeyup="multiplicalf(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="f5" class="txt" TYPE="text" SIZE="3" NAME="f5" onkeyup="multiplicalf(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="f6" class="txt" TYPE="text" SIZE="3" NAME="f6" onkeyup="multiplicalf(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="f7" class="txt" TYPE="text" SIZE="3" NAME="f7" onkeyup="multiplicalf(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="f8" class="txt" TYPE="text" SIZE="3" NAME="f8" onkeyup="multiplicalf(), multiplicatotal()" value="0"></td>
             <td align="right" ><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total_lf" value="0" disabled></td>
		</tr>
        <tr id="8" bgcolor="#dee0e1" style="display: none;" >
        <!-- Linha G-->
          <td><label> <input type="text" name="" value="Descrição" id="" class="txt"/>
</label></td>
          <td><INPUT id="g1" class="txt" TYPE="text" SIZE="3" NAME="g1" onkeyup="multiplicalg(), multiplicatotal()" value="0"></td>
		  <td style="display: none;"><INPUT id="g2" class="txt" TYPE="text" SIZE="3" NAME="g2" onkeyup="multiplicalg(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="g3" class="txt" TYPE="text" SIZE="3" NAME="g3" onkeyup="multiplicalg(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="g4" class="txt" TYPE="text" SIZE="3" NAME="g4" onkeyup="multiplicalg(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="g5" class="txt" TYPE="text" SIZE="3" NAME="g5" onkeyup="multiplicalg(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="g6" class="txt" TYPE="text" SIZE="3" NAME="g6" onkeyup="multiplicalg(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="g7" class="txt" TYPE="text" SIZE="3" NAME="g7" onkeyup="multiplicalg(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="g8" class="txt" TYPE="text" SIZE="3" NAME="g8" onkeyup="multiplicalg(), multiplicatotal()" value="0"></td>
             <td align="right"><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total_lg" value="0" disabled></td>
		</tr>
        <tr id="9" style="display: none;" >
        <!-- Linha H-->
          <td ><label> <input type="text" name="" value="Descrição" id="" class="txt"/>
</label></td>
          <td ><INPUT id="h1" class="txt" TYPE="text" SIZE="3" NAME="h1" onkeyup="multiplicalh(), multiplicatotal()" value="0"></td>
		  <td style="display: none;"><INPUT id="h2" class="txt" TYPE="text" SIZE="3" NAME="h2" onkeyup="multiplicalh(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="h3" class="txt" TYPE="text" SIZE="3" NAME="h3" onkeyup="multiplicalh(), multiplicatotal()" value="0"></td>
			<td style="display: none;"><INPUT id="h4" class="txt" TYPE="text" SIZE="3" NAME="h4" onkeyup="multiplicalh(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="h5" class="txt" TYPE="text" SIZE="3" NAME="h5" onkeyup="multiplicalh(), multiplicatotal()" value="0"></td>
            <td width="8%" style="display: none;"><INPUT id="h6" class="txt" TYPE="text" SIZE="3" NAME="h6" onkeyup="multiplicalh(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="h7" class="txt" TYPE="text" SIZE="3" NAME="h7" onkeyup="multiplicalh(), multiplicatotal()" value="0"></td>
            <td width="10%" style="display: none;"><INPUT id="h8" class="txt" TYPE="text" SIZE="3" NAME="h8" onkeyup="multiplicalh(), multiplicatotal()" value="0"></td>
             <td align="right" ><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total_lh" value="0" disabled></td>
		</tr>
         
	</table>
    
<tr>

<table width="89%" >
<td width="685" class="sub"><input type="button" class="btnrel" onClick="decrementarlinha()" value="Diminuir Linha"> 
<input type="button" class="btnrel" onClick="incrementarlinha()" value="Aumentar Linha"> 
<input type="button" class="btnrel" onClick="decrementarcoluna()" value="Diminuir Coluna"> 
<input type="button" class="btnrel" onClick="incrementarcoluna()" value="Aumentar Coluna"></td>

<td width="61"  class="sub">TOTAL:</td>
    <td width="72" bgcolor="#dee0e1" align="right" class="sub"><span> R$</span><INPUT TYPE="text" SIZE="3" NAME="total" value="0" disabled></td>
  </tr>
</table>

  <input type="submit" value="Cadastrar" class="btn" >
</form>
<p>&nbsp;</p>

<?php
if(@$_GET['go'] == 'cadastrar2'){
		  

//primeira linha
$valorLinhaA1 = $_POST['a1'];//dados coluna a1 linha1
$datacol1 = $d1; //dados data da coluna 1
$descricao1 = $_POST['descricao1']; // dados descrição da prmeira linha 

//segunda linha
$valorLinhaB1 = $_POST['b1'];//dados coluna b1 linha1
$datacol1 = $d1; //dados data da coluna 1
$descricao2 = $_POST['descricao2']; // dados descrição da prmeira linha 


			 mysql_query(" insert into adiantamentorelatorio (descrição, data, valor)
 values ('$valorLinhaA1', $datacol1 ,'$descricao2'); 
 
 ");

			echo "<script>alert('Viagem cadastrado com sucesso.');</script>";
		
	}
?>
    
asked by anonymous 24.01.2016 / 15:34

1 answer

1

You can check if there are values in all inputs and then insert if there is value, so pick the letter order and have the date / day.

I've made an EXTREMELY COMMENTED code so you can understand step-by-step:

<?php

$NomeColunas = range('a', 'h');
// Irá retornar uma array com o alfabeto (abcdefgh), se adicionar mais basta alterar o "h".

$iNum = 1;
// LINHA : Isso será utilizado para obter o número em a>1< ou a>2<, exemplo. 

$iLetra = 0;
// COLUNA : Isso será utilizado para obter a possição da letra, em conjunto com o $NomeColunas será (0 => a, 1 => b).


while($iLetra <= count($NomeColunas)){
// Irá criar um loop finito para cada letra/coluna existente

$LetraAtual = $NomeColunas[$iLetra];
// Letra atual (a,b,c)



if( isset($_POST[ $LetraAtual.$iNum ]) && $_POST[ $LetraAtual.$iNum ] != ''){
// Se existir um POST da coluna e linha (a1, a2...)


$Descricao = $_POST[$LetraAtual.'DESC'];
// Descrição será aDESC, bDESC porque é fixo por linha!
// /!\ Nota requer modificação do name='' no HTML!

$Data = date('d-m-Y', strtotime("+". $iLetra ." days"));
// SUPONDO que $d1 da primeira coluna seja hoje, da segunda amanhã... Seria isto!
// a1 = 25-01-2016, a2 = 26-01-2016...

$Valor = $_POST[ $LetraAtual.$iNum ];
// Pega valor :: a1 (a vem do $LetraAtual e 1 vem do $iNum)

mysql_query("INSERT INTO adiantamentorelatorio (descrição, data, valor) VALUES ('$Valor', '$Data' ,'$Descricao')");
// Insere os dados.

}
// Fim do IF



if($iNum == 8){
    // Se for a ultima linha (8)
    $iNum = 1;
    // Volta para primeiro numero/linha.
    $iLetra++;
    // Passa para proxima letra/coluna.
}else{
    // Se não
    $iNum++;
    // Passa para proxima linha na mesma coluna
}



}
// Fim do while


?>

Goal:

  

Insert all the data if there is a POST related to it.

Notes:

  

Requires HTML modifications for each Description to have name='aDESC' , name = 'bDESC' .

     

mysql_* is deprecated and is not recommended.

     

Your code is functional but is (in my opinion) badly constructed. There are repetitions done manually, which you could have done with the while / for / foreach, for example.

     

I tried to comment as much as possible so that I can understand the process and can even use it to subscribe to these manual repetitions.

    
25.01.2016 / 11:21