Update form not saved

Asked

Viewed 99 times

1

I have a form already filled with two extra inputs only for the user to edit the information already registered and add the two new.

However, although the alert of "successfully saved", when I consult the bank the information has not been changed nor the new added.

My form:

<?php
    require 'strcon.php';
    $query = mysqli_query($strcon, "SELECT SERVICO FROM pedidos");
    $cliente = filter_input(INPUT_POST, 'CLIENTE');
    $servico = filter_input(INPUT_POST, 'SERVICO');
    $solicitacao = filter_input(INPUT_POST, 'SOLICITACAO');
    $previsao = filter_input(INPUT_POST, 'PREVISAO');
    $valor = filter_input(INPUT_POST, 'VALOR');
    $acerto = filter_input(INPUT_POST, 'ACERTO');
    $saldo = filter_input(INPUT_POST, 'SALDO');
    $id = filter_input(INPUT_POST, 'ID');
?>

    <!-- formulário -->
    <form method="POST" action="update-edi.php">
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <div class="form-group">
            <label for="CLIENTE">Cliente:</label>
            <input type="text" class="form-control" id="CLIENTE" name="CLIENTE" value="<?php echo $cliente; ?>">
          </div>
          <div class="form-group">
            <label for="SERVICO">Serviço:</label>
            <input type="text" class="form-control" id="SERVICO" name="SERVICO" value="<?php echo $servico; ?>">
          </div>
          <div class="form-group">
            <label for="SOLICITACAO">Data de solicitação:</label>
            <input type="text" class="form-control" id="SOLICITACAO" name="SOLICITACAO" value="<?php echo $solicitacao; ?>">
          </div>
          <div class="form-group">
            <label for="PREVISAO">Data prevista:</label>
            <input type="text" class="form-control" id="PREVISAO" name="PREVISAO" value="<?php echo $previsao; ?>">
          </div>
          <div class="form-group">
            <label for="VALOR">Valor:</label>
            <input type="text" class="form-control" id="VALOR" name="VALOR" value="<?php echo $valor; ?>">
          </div>
          <div class="form-group">
            <label for="ACERTO">Acerto:</label>
            <input type="text" class="form-control" id="ACERTO" name="ACERTO" value="<?php echo $acerto; ?>">
          </div>
          <div class="form-group">
            <label for="SALDO">Saldo:</label>
            <select type="text" class="form-control" id="SALDO" name="SALDO" value="<?php echo $saldo; ?>">
              <option> Selecione... </option>
              <option> Positivo </option>
              <option> Negativo </option>
              <option> Neutro </option>
            </select>
          </div>
          <button type="submit" class="btn btn-primary btn-lg btn-block">Salvar</button>
        </div>
      </div>
    </div>
    </form>

My update page:

<?php

    $acerto = filter_input(INPUT_POST, 'ACERTO');
    $saldo = filter_input(INPUT_POST, 'SALDO');
    $id = filter_input(INPUT_POST, 'ID');

    $strcon = mysqli_connect('localhost', 'root', '', 'sis_tam') or die('Erro ao conectar ao banco de dados');
    $sql = 'UPDATE pedidos SET ACERTO = " '. $acerto . ' ",  SALDO = " '. $saldo . ' "  WHERE ID = " '. $id . ' " ';
    mysqli_query($strcon,$sql) or die("Erro ao tentar atualizar registro. " . mysqli_error($strcon));
    mysqli_close($strcon);

    echo '<script type="text/javascript">
                alert("Salvo com Sucesso !");
                window.history.go(-1);
            </script>';

    var_dump($acerto, $saldo, $id);

?>

I don’t understand why you’re not saving, if anyone understands, I thank you. :)

  • 1

    There is an extra space in the concatenation, what type of these fields?

  • 1

    The alert will always be triggered regardless of whether or not there was an update. Use the result of mysqli_affected_rows($strcon) to trigger the alert if yes. Learn more at http://php.net/manual/en/mysqli.affected-rows.php Already, the update problem seems to me that Roberto de Campos has already detected the error.

  • On the update page you are recovering an ID $id = filter_input(INPUT_POST, 'ID'); not passed by the form.

2 answers

1

Turns out there’s nothing being found in his WHERE, has to remove the extra spaces in the query:

$sql = 'UPDATE pedidos SET ACERTO = "'. $acerto . '",  SALDO = "'. $saldo . '"  WHERE ID = "'. $id . '" ';

You were comparing 5 with 5 for example if the id was the same as 5.

  • It seems that was not the problem. I copied your query but the problem remains. :/

  • As for the spaces, what will occur is: values with spaces will be inserted in the columns. As for the spaces in the WHERE condition: a conversão numérica ignora os espaços. Não funcionaria se fossem outros caracteres. Se o campo for char, ai vai dar problema.

0

On the update page you are recovering an ID $id = filter_input(INPUT_POST, 'ID'); not passed by the form.

Add to the form page a input which may be Hidden or text or number:

      <div class="form-group">
        <label for="id">ID:</label>
        <input type="hidden" class="form-control" id="ID" name="ID" value="<?php echo $id; ?>">
      </div>

Spaces in the declaration UPDATE what will occur is:

  1. Values with spaces will be inserted in the columns.
  2. In the WHERE condition: the numeric conversion ignores the spaces. It wouldn’t work if they were other characters. If the field is char, there will be a problem.

So, if the ID is numerical the query will be performed successfully.

Browser other questions tagged

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