Change database information via PHP

Asked

Viewed 27 times

-1

I made two files in order to alter the information of an item with a specific id, the code works correctly, but the information is not changed. Can you help me? Follow below the codes:

edit.php:

<html>
<head>
</head>
<body>
<a href="formulario.html">Cadastrar um ativo</a>
<br>
<a href="listar.php">Listar Ativos</a>
<br><br>
<form method="POST" action="formulario_editar.php">
    ID: <br><input type="text" name="id"><br><br>
    Empresa: <br><input type="text" name="empresa"><br><br>
    Filial: <br><input type="text" name="filial"><br><br>
    Segmento: <br><input type="text" name="segmento"><br><br>
    <input type="submit" value="Editar">
</form> 

<br>
<br>
</body>
</html>

formulario_edit.php:

<html>
<head>
</head>
<body>
<a href="formulario.html">Cadastrar um ativo</a>
<br>
<a href="listar.php">Listar Ativos</a>
<br><br>
</body>
</html>

<?php

$servername = "localhost";
$database = "tedinfo1_app";
$username = "tedinfo1_app";
$password = "123456789";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
}

$id = $_POST['id'];
$empresa = $_POST['empresa'];
$filial = $_POST['filial'];
$segmento = $_POST['segmento']; 

$result_empresa = "UPDATE ativos SET empresa = '$empresa' THERE id = '$id'";
$resultado_empresa = mysqli_query ($conn, $result_empresa);

$result_filial = "UPDATE ativos SET filial = '$filial' THERE id = '$id'";
$resultado_filial = mysqli_query ($conn, $result_filial);

$result_segmento = "UPDATE ativos SET segmento = '$segmento' THERE id = '$id'";
$resultado_segmento = mysqli_query ($conn, $result_segmento);

echo "Ativo editado com súcesso!";

?>

1 answer

0

You wrote THERE but it should be WHERE. Follows modified code:

<?php

$servername = "localhost";
$database = "tedinfo1_app";
$username = "tedinfo1_app";
$password = "123456789";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
}

$id = $_POST['id'];
$empresa = $_POST['empresa'];
$filial = $_POST['filial'];
$segmento = $_POST['segmento']; 

$result_empresa = "UPDATE ativos SET empresa = '$empresa' WHERE id = '$id'";
$resultado_empresa = mysqli_query ($conn, $result_empresa);

$result_filial = "UPDATE ativos SET filial = '$filial' WHERE id = '$id'";
$resultado_filial = mysqli_query ($conn, $result_filial);

$result_segmento = "UPDATE ativos SET segmento = '$segmento' WHERE id = '$id'";
$resultado_segmento = mysqli_query ($conn, $result_segmento);

echo "Ativo editado com súcesso!";

?>

  • Solved! Thanks for the tip!

  • @Ruan don’t forget to check if the POST field is empty with isset ...

Browser other questions tagged

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