Update does not give error but does not update in the database

Asked

Viewed 1,226 times

-4

Why don’t you do UPDATE in the database? The code does not give error but does not update.

<?php
elseif ($op === "atualizar")
{   
    //Atualizar arquivos
    $id = $_POST["id"];
    $nome = trim($_POST["nome"]);
    $tipo = $_POST["tipo"];
    if ($nome == "")
    {
        $mensagem = "O campo deve ser preenchido";
    }
    else
    {
        $sql = "UPDATE veiculo SET nome = '$nome', tipo = $tipo ";
        $result = mysql_query($sql, $conn);
        if ($result)
        {
            header("location:veiculoLista.php");
            exit;
        }
        else $mensagem = "Não foi possível atualizar. Verifique os dados!";
    }
}
?

<html>
    <head>
        <title>Veiculo cadastro</title>
    </head>
    <body>
        <h1>Cadastro de veiculo </h1>
        <font color="red"><?php if(isset($mensagem)) echo($mensagem);?></font>

            <form method="post" action="veiculo.php" name="fveiculo">

                <label>Nome</label><br />
                <input name="nome" type="text" value="<?php echo $nome; ?>" size="45"/>
                <br />
                <label>Tipo</label>
                <br />
                <select name="tipo" size="1">
        <option value="1" <?php if($tipo == 1) echo " selected"; ?>>Basico</option>
            <option value="2" <?php if($tipo == 2) echo " selected"; ?>>Avancado com opcionais</option>
                </select><br />

                    <?php if($op != "cadastrar"){?>
                            <input type="checkbox" name="excluir" value="excluir">Excluir<br>
                     <?php }?>

                     <?php if($op == "atualizar"){?>
                        <input type="hidden" name="id" value="<?php echo $id ?>">
                     <?php }?>

                    <input type="hidden" name="op" value="<?php echo $op ?>">
                    <input type="submit" value="Salvar"/>
                    <br />
                    <a href="javascript:void(null);" onclick="location.href='veiculoLista.php';">Voltar</a>
            </form>

    </body>
</html>
  • What is the specific problem you have? What error does it make? So it is difficult to help you.

  • The code appears to be incomplete, put the rest before "Else if". And be more objective in your question, read here http://answall.com/help/how-to-ask

  • When you spin your code it falls into the if($result) or in the else?

  • The strange thing is code start with a elseif. So, what is the question exactly ? Because you can’t understand much. The form is filled with information in advance registered in the database ? Or simply record new information as your headers ? Explain better what you intend to do.

  • It’s just an excerpt from the code

1 answer

3


You are not putting the Where condition in your update, the way it is, it updates everything, change the sql to =

$sql = "UPDATE veiculo SET nome = '$nome', tipo = $tipo WHERE id = $id";

I advise you to stop using the mysql_* and use PDO.

  • 2

    +1 for wise counsel of the Pdo

  • 1

    Thanks @henriquedpereira worked, this even keeps appearing a message saying it will be discounted.

Browser other questions tagged

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