Update query data in php

0

I've been looking at dozens of topics, but I still can not find a solution.

I created a form that shows the data of the database table and when the data is changed I need to update the table row.

This is the code I have but does not do the update in the table:

<form action="" method="post">
  <label>Insira a data:</label>
  <input name="data" type="date" placeholder="Type Here">
  <br>
  <br>
  <input type="submit" value="Enter">
</form>
<?php  
if(isset($_POST['data']))
{

$servername = "xxx.xxx.x.xx";
$username = "xxxxx";
$password = "xxxxxxxxx";
$dbname = "xxxxxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');


    $inicio = mysqli_real_escape_string($conn, $_POST['data']);  


$sql = "SELECT 'regOratorio'.'DataEntrada',
    'regOratorio'.'Utente',
    'regOratorio'.'Estado',
    'regOratorio'.'Observacao',
'InfoLuvas'.'Funcionario'
FROM 'centrodb'.'regOratorio' LEFT OUTER JOIN 'centrodb'.'InfoLuvas'

ON 'centrodb'.'InfoLuvas'.'Id' = 'centrodb'.'regOratorio'.'Colaborador' WHERE DataEntrada = '$inicio'";

 $result=mysqli_query($conn, $sql);

 if (!$result) {
      echo 'There are no results for your search';
  } else {
    // result to output the table
    echo '<form name="customer_details" action="" method="POST" onsubmit="return alguma_funcao()">';    
  }

  while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{   
     echo "<fieldset>";
  echo "<table cellspacing='10'>";
   echo "<tr>";
   echo "<td>";
  echo "<label>Data</label>"; 
  echo "<input type='date' id='DataEntrada' name='DataEntrada' value='".$row['DataEntrada']."'";
   echo "</td>";
   echo "</tr>";
    echo "</table>";
   echo "</fieldset>";

  echo "<fieldset>";
  echo "<table cellspacing='10'>";
   echo "<tr>";
   echo "<td>";
  echo "<label>Utente</label>"; 
  echo "<input id='' type='text' value='".$row['Utente']."'";

  echo "<label>Estado</label>"; 
  echo "<input type='radio' id='Estado' name='Estado' value='Presente' " . ( ($row['Estado']=='Presente') ? 'checked' : '' ) ."> Presente";
  echo "<input type='radio' id='Estado' name='Estado' value='Ausente' " . ( ($row['Estado']=='Ausente') ? 'checked' : '' ) ."> Ausente";

  echo "<label>Observacão</label>"; 
  echo "<input type='text' id='Observacao' name='Observacao' value='".$row['Observacao']."'";
  echo "</td>";
echo "</tr>";
    echo "</table>";
   echo "</fieldset>";

   echo "<fieldset>";
  echo "<table cellspacing='10'>";
   echo "<tr>";
   echo "<td>";
  echo "<label>Colaborador</label>"; 
  echo "<input type='text' id='Funcionario' name='Funcionario' value='".$row['Funcionario']."'";
  echo "</td>";
echo "</tr>";
    echo "</table>";
   echo "</fieldset>";

   echo "<input name='atualizar' type='submit' id='atualizar' value='Atualizar'>";
 }
echo "</form>";
}// end submitted

?>

<?php

if(isset($_POST['atualizar']))
{

$data = $_POST['DataEntrada'];
$estado = $_POST['Estado'];
$observacao = $_POST['Observacao'];
$estadofinal = $_POST['EstadoFinal'];

echo $sql1 = "UPDATE regOratorio SET Estado = '$estado', Observacao = '$observacao', EstadoFinal = '$estadofinal' WHERE DataEntrada = '$data'";

if ($conn->query($sql1) === TRUE);

    //Count total number of rows
    $rowCount = $query->num_rows;

}
$conn->close();

?>

When I update it shows this data inside if(isset($_POST['atualizar']))

    
asked by anonymous 07.02.2018 / 13:22

1 answer

0

The problem was the location of update .

Structure to work:

First the form to search for the date you want to see:

<form action="" method="post">
  <label>Insira a data:</label>
  <input name="data" type="date" placeholder="Type Here">
  <br>
  <br>
  <input type="submit" value="Enter">
</form>

Then the method of update :

<?php  
if(isset($_POST['atualizar']))
{

$servername = "xxx.xxx.x.xx";
$username = "xxxxx";
$password = "xxxxxxxx";
$dbname = "xxxxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');

$data = $_POST['DataEntrada'];
$estado = $_POST['Estado'];
$observacao = $_POST['Observacao'];
$estadofinal = $_POST['EstadoFinal'];

$sql1 = "UPDATE teste SET Estado = '$estado', Observacao = '$observacao', EstadoFinal = '$estadofinal' WHERE DataEntrada = '$data'";

if ($conn->query($sql1) === TRUE);

    //Count total number of rows
    $rowCount = $query->num_rows;



$conn->close();

}// end submitted

?>

Now comes the query method where it shows the data in the form:

<?php  
if(isset($_POST['data']))
{

$servername = "xxx.xxx.x.xx";
$username = "xxxxx";
$password = "xxxxxxxx";
$dbname = "xxxxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');


    $inicio = mysqli_real_escape_string($conn, $_POST['data']);  


$sql = "SELECT 'teste'.'Id',
    'teste'.'DataEntrada',
    'teste'.'Utente',
    'teste'.'Estado',
    'teste'.'Observacao',
    'InfoLuvas'.'Funcionario'
FROM 'centrodb'.'teste' LEFT OUTER JOIN 'centrodb'.'InfoLuvas'

ON 'centrodb'.'InfoLuvas'.'Id' = 'centrodb'.'teste'.'Colaborador' WHERE DataEntrada = '$inicio'";

$result=mysqli_query($conn, $sql);

 if (!$result) {
      echo 'There are no results for your search';
  } else {
    // result to output the table
    echo '<form name="customer_details" action="" method="POST" onsubmit="return alguma_funcao()">';    
  }

  while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{   
     echo "<fieldset>";
  echo "<table cellspacing='10'>";
   echo "<tr>";
   echo "<td>";
   echo "<label>Número de Registo</label>"; 
  echo "<input type='text' id='Id' name='Id' value='".$row['Id']."'";
   echo "</td>";
   echo "<td>";
  echo "<label>Data</label>"; 
  echo "<input type='date' id='DataEntrada' name='DataEntrada' value='".$row['DataEntrada']."'";
   echo "</td>";
   echo "</tr>";
    echo "</table>";
   echo "</fieldset>";

  echo "<fieldset>";
  echo "<table cellspacing='10'>";
   echo "<tr>";
   echo "<td>";
  echo "<label>Utente</label>"; 
  echo "<input id='' type='text' value='".$row['Utente']."'";

  echo "<label>Estado</label>"; 
  echo "<input type='radio' id='Estado' name='Estado' value='Presente' " . ( ($row['Estado']=='Presente') ? 'checked' : '' ) ."> Presente";
  echo "<input type='radio' id='Estado' name='Estado' value='Ausente' " . ( ($row['Estado']=='Ausente') ? 'checked' : '' ) ."> Ausente";

  echo "<label>Observacão</label>"; 
  echo "<input type='text' id='Observacao' name='Observacao' value='".$row['Observacao']."'";
  echo "</td>";
  echo "</tr>";
    echo "</table>";
   echo "</fieldset>";

   echo "<fieldset>";
  echo "<table cellspacing='10'>";
   echo "<tr>";
   echo "<td>";
  echo "<label>Colaborador</label>"; 
  echo "<input type='text' id='Funcionario' name='Funcionario' value='".$row['Funcionario']."'";
  echo "</td>";
  echo "<td>";
  echo "<label>Estado</label>"; 
  echo "<input type='radio' id='EstadoFinal' name='EstadoFinal' value='Concluído' " . ( ($row['EstadoFinal']=='Concluído') ? 'checked' : '' ) ."> Concluído";
   echo "</td>";
   echo "</tr>";
    echo "</table>";
   echo "</fieldset>";

   echo "<input name='atualizar' type='submit' id='atualizar' value='Atualizar'>";
 }
echo "</form>";

$conn->close();

}// end submitted

?>
    
08.02.2018 / 11:44