Update query data in php

Asked

Viewed 379 times

-1

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

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

this is the code I have but does not 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 the if(isset($_POST['atualizar']))

inserir a descrição da imagem aqui

  • Have you checked if you’re entering the if(isset($_POST['atualizar']))?

  • How can I verify, with the echo?

  • Yes, you can put an echo inside the if, if it shows nothing it is because it is not entering, so it will not update.

  • I put in question the result of when I update with the echo and I think it goes into the if(isset($_POST['atualizar'])), but does not update data within the table

  • remove the echo... echo is for printing on the screen

  • I removed the echo but does not update the table row in the same

  • Updates on which line?

  • I explained wrong above, does not update anything in the table in any row

  • The problem may be in the WHERE that is not finding the date. What is the type of the field DataEntrada on the table?

  • It’s the primary key

  • But it’s like date, datetime, varchar?

  • It’s like date, that’s the problem?

  • removed the echo from here huh ? echo $sql1 ...

  • yes I withdrew....

  • Are you sure there is the date '2018-02-05' in the table?

  • Yes there is...is the only line I have on the table

  • You do not have to run the $sql1 query before if?

  • Do not update the table row in the same way

Show 13 more comments

1 answer

-1

The problem was the location of update.

Working structure:

First the form to find the date you want to consult:

<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

?>

Browser other questions tagged

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