MySQL UPDATE function is editing right, but creates a new empty line at the end of the table [closed]

2

I'm breaking my head here, because the UPDATE function works fine when used purely by the terminal or the Workbench, but when I do the PHP code via HTML form parameters, it edits fine too, but creates a new line empty, and will create more and more empty lines every time you edit something. And this does not happen via Terminal and Workbench. (I thought it had something to do with the date in TIMESTAMP, so I deleted the date field from DB and PHP but the problem continued.)

<h1> Editar </h1>
<form method="post" action="<?php require('editar.php'); echo htmlspecialchars($_SERVER["PHP_SELF"]) ?>" >

id: <br><input type="number" name="id_upd">
<br><br>

Nome: <br><input type="text" name="nome_upd">
<br><br>

Localização: <br><input type="text" name="localizacao_upd">

<br><br>

E-mail: <br><input type="text" name="email_upd">
<br><br>

Website: <br><input type="text" name="website_upd">
<br><br>

Gênero:
<input type="radio" name="genero_upd" value="Feminino">Feminino
<input type="radio" name="genero_upd" value="Masculino">Masculino

<br><br>

Mensagem:<br> 
<textarea name="mensagem_upd" rows="10" cols="50"></textarea>
<br>

<input type="submit" name="submit" value="Submit"> 

<?php
$servername = "localhost";
$username = "root";
$password = "*******";
$dbname = "guestbook";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $edit = $id_upd = $nome_upd = $localizacao_upd = $email_upd = $website_upd = $genero_upd = $mensagem_upd = "";

//Inserindo os dados de entrada do usuário recebidos do formulário HTML nas váriaveis
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $id_upd = $_POST["id_upd"];
    $nome_upd = $_POST["nome_upd"];
    $localizacao_upd = $_POST["localizacao_upd"];
    $email_upd = $_POST["email_upd"];
    $website_upd = $_POST["website_upd"];
    $genero_upd = $_POST["genero_upd"];
    $mensagem_upd = $_POST["mensagem_upd"];

}

//Se a entrada do usuário for diferente de "" (vazio) edite o nome para nova entrada.
if($nome_upd != "") {
        //Variável $sql recebe a notação de UPDATE MySQL com parâmetros (os valores serão a entrada do usuário)
        $sql = "UPDATE guestbook SET nome='".$nome_upd."' WHERE id='".$id_upd."'";

        //Preparação de confirmação individual
        $stmt = $conn->prepare($sql);

        //Execução da preparação individual
        $stmt->execute();
}

if($localizacao_upd != "") {
        $sql = "UPDATE guestbook SET localizacao='".$localizacao_upd."' WHERE id='".$id_upd."'";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
}

if($email_upd != "") {
        $sql = "UPDATE guestbook SET email='".$email_upd."' WHERE id='".$id_upd."'";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
}

if($website_upd != "") {
        $sql = "UPDATE guestbook SET website='".$website_upd."' WHERE id='".$id_upd."'";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
}

if($genero_upd != "") {
        $sql = "UPDATE guestbook SET genero='".$genero_upd."' WHERE id='".$id_upd."'";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
}

 if($mensagem_upd != "") {
        $sql = "UPDATE guestbook SET mensagem='".$mensagem_upd."' WHERE id='".$id_upd."'";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
 }
}
catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

    
asked by anonymous 20.10.2015 / 22:42

1 answer

0

Editing usually works that way, by clicking on the link on the listing page the user is redirected to editar.php , it searches the database for the id, which was passed by $_GET .

Listing.php

$row is a listing of all records, has a while / foreach before that link.

<a href="cadastros/editar.php?id=<?php echo $row['id']>EDITAR</a>"

Edit.php

Do the search by id, with the value received in $_GET['id'] , then close the php tag, create the form html, in each field add the value attribute and open the php tag and type the value of its field with $registro['nome_do_campo'] .

I recommend creating a new file ( gravar.php ) to perform the update and insert.

How do you know when to include or update the registry? If there is a value in the field id_udp it is an update of the opposite is an insert.

<?php
    include 'conexao.php';

    $id = !empty($_GET['id']) ? $_GET['id'] : 0;

    $sql = "SELECT * FROM guestbook WHERE id = :id"
    $stmt = $db->prepare($sql);
    $stmt->bindValue(':id', $id);
    $stmt->execute();

    $registro = $stmt->fetch(PDO::FETCH_ASSOC);

?>

<form method="post" action="gravar.php">

id: <br><input type="hidden" name="id_upd" value="<?php echo $registro['id'];" />
<br><br>

Nome: <br>
      <input type="text" name="nome_upd" value="<?php echo $registro['nome_upd'];" />
<br><br>

Localização: <br><input type="text" name="localizacao_upd" value="<?php echo $registro['localizacao_upd'];" />  

demais campos ...

</form> 
    
21.10.2015 / 19:58