Changing Mysql data in PHP

Asked

Viewed 13,056 times

1

I have the Tb_workers where it is filled in by a form. And then I created another page to change this data.

Altering.php

$id = $exibe["id"];

$Nome = $exibe["Nome"];

$Morada = $exibe["Morada"];

$Tipo = $exibe["Tipo"];

$Email = $exibe["Email"];
$AlvaraNumero = $exibe["AlvaraNumero"];

$AlvaraValidade = $exibe["AlvaraValidade"];

$AlvaraAnexo = $exibe["AlvaraAnexo"];

$AcidenteNumero = $exibe["AcidenteNumero"];

$AcidenteValidade = $exibe["AcidenteValidade"];

 $AcidenteAnexo = $exibe["AcidenteAnexo"];

 $SeguroNumero = $exibe["SeguroNumero"];

$SeguroValidade = $exibe["SeguroValidade"];

 $SeguroAnexo = $exibe["SeguroAnexo"];

$FinancasValidade = $exibe["FinancasValidade"];

 $FinancasAnexo = $exibe["FinancasAnexo"];

 $SocialValidade = $exibe["SocialValidade"];

$SocialAnexo = $exibe["SocialAnexo"];

$RemuneracaoValidade = $exibe["RemuneracaoValidade"];

$RemuneracaoAnexo = $exibe["RemuneracaoAnexo"];

$InstaladorNumero = $exibe["InstaladorNumero"];

$InstaladorValidade = $exibe["InstaladorValidade"];

$InstaladorAnexo = $exibe["InstaladorAnexo"];

 $MontadorNumero = $exibe["MontadorNumero"];

$MontadorValidade = $exibe["MontadorValidade"];

$MontadorAnexo = $exibe["MontadorAnexo"];
}

?>

 <form action="salvaralteracao.php" method="POST">
    <input type="hidden" name="id" value="<?php echo $id; ?>">
    Nome<input type="Varchar" name="Nome" value="<?php echo $Nome; ?>"><p>
    Morada<input type="Text" name="Morada" value="<?php echo $Morada; ?>"><p>
    Email<input type="text" name="Email" value="<?php echo $Email; ?>"><p>
            AlvaraNumero<input type="integer" name="AlvaraNumero" value="<?php echo $AlvaraNumero; ?>"><p>
    AlvaraValidade<input type="date" name="AlvaraValidade" value="<?php echo $AlvaraValidade; ?>"><p>
    AlvaraAnexo<input type="file" name="AlvaraAnexo" value="<?php echo $AlvaraAnexo; ?>"><p>
    AcidenteNumero<input type="integer" name="AcidenteNumero" value="<?php echo $AcidenteNumero; ?>"><p>
    AcidenteValidade<input type="Date" name="AcidenteValidade" value="<?php echo $AcidenteValidade; ?>"><p>
    AcidenteAnexo<input type="file" name="AcidenteAnexo" value="<?php echo $AcidenteAnexo; ?>"><p>
    SeguroNumero<input type="integer" name="SeguroNumero" value="<?php echo $SeguroNumero; ?>"><p>
    SeguroValidade<input type="Date" name="SeguroValidade" value="<?php echo $SeguroValidade; ?>"><p>
    SeguroAnexo<input type="file" name="SeguroAnexo" value="<?php echo $SeguroAnexo; ?>"><p>
    FinancasValidade<input type="Date" name="FinancasValidade" value="<?php echo $FinancasValidade; ?>"><p>
    FinancasAnexo<input type="file" name="FinancasAnexo" value="<?php echo $FinancasAnexo; ?>"><p>
    SocialValidade<input type="Date" name="SocialValidade" value="<?php echo $SocialValidade; ?>"><p>
    SocialAnexo<input type="file" name="SocialAnexo" value="<?php echo $SocialAnexo; ?>"><p>
    RemuneracaoValidade<input type="Date" name="RemuneracaoValidade" value="<?php echo $RemuneracaoValidade; ?>"><p>
    RemuneracaoAnexo<input type="file" name="RemuneracaoAnexo" value="<?php echo $RemuneracaoAnexo; ?>"><p>
    InstaladorNumero<input type="integer" name="InstaladorNumero" value="<?php echo $InstaladorNumero; ?>"><p>
    InstaladorValidade<input type="Date" name="InstaladorValidade" value="<?php echo $InstaladorValidade; ?>"><p>
    InstaladorAnexo<input type="file" name="InstaladorAnexo" value="<?php echo $InstaladorAnexo; ?>"><p>
    MontadorNumero<input type="integer" name="MontadorNumero" value="<?php echo $MontadorNumero; ?>"><p>
    MontadorValidade<input type="Date" name="MontadorValidade" value="<?php echo $MontadorValidade; ?>"><p>
    MontadorAnexo<input type="file" name="MontadorAnexo" value="<?php echo $MontadorAnexo; ?>"><p>      
 <input type="submit" value="Guardar">

 </form>

php save.

<?php

include("conectar.php");

$id = $_POST['id'];

$Nome = $_POST['Nome'];

$Morada = $_POST['Morada'];

$Email = $_POST['Email'];

    $AlvaraNumero = $_POST["AlvaraNumero"];

$AlvaraValidade = $_POST["AlvaraValidade"];

$AlvaraAnexo = $_POST["AlvaraAnexo"];

$AcidenteNumero = $_POST["AcidenteNumero"];

$AcidenteValidade = $_POST["AcidenteValidade"];

$AcidenteAnexo = $_POST["AcidenteAnexo"];

$SeguroNumero = $_POST["SeguroNumero"];

$SeguroValidade = $_POST["SeguroValidade"];

$SeguroAnexo = $_POST["SeguroAnexo"];

$FinancasValidade = $_POST["FinancasValidade"];

$FinancasAnexo = $_POST["FinancasAnexo"];

$SocialValidade = $_POST["SocialValidade"];

$SocialAnexo = $_POST["SocialAnexo"];

$RemuneracaoValidade = $_POST["RemuneracaoValidade"];

$RemuneracaoAnexo = $_POST["RemuneracaoAnexo"];

$InstaladorNumero = $_POST["InstaladorNumero"];

$InstaladorValidade = $_POST["InstaladorValidade"];

$InstaladorAnexo = $_POST["InstaladorAnexo"];

$MontadorNumero = $_POST["MontadorNumero"];

$MontadorValidade = $_POST["MontadorValidade"];

$MontadorAnexo = $_POST["MontadorAnexo"];


  $sqlinsert = "Update tb_trabalhador SET Nome='$Nome' ,Morada='$Morada',Email='$Email' AlvaraNumero='$AlvaraNumero',AlvaraValidade='$AlvaraValidade',AlvaraAnexo='$AlvaraAnexo',AcidenteNumero='$AcidenteNumero',cidenteValidade='$AcidenteValidade',AcidenteAnexo='$AcidenteAnexo',SeguroNumero='$SeguroNumero',SeguroValidade='$SeguroValidade',SeguroAnexo='$SeguroAnexo',FinancasValidade='$FinancasValidade',FinancasAnexo='$FinancasAnexo',SocialValidade='$SocialValidade',SocialAnexo='$SocialAnexo',RemuneracaoValidade='$RemuneracaoValidade',RemuneracaoAnexo='$RemuneracaoAnexo',InstaladorNumero='$InstaladorNumero',InstaladorValidade='$InstaladorValidade',InstaladorAnexo='$InstaladorAnexo',MontadorNumero='$MontadorNumero',MontadorValidade='$MontadorValidade',MontadorAnexo='$MontadorAnexo' where id=$id ";
  mysql_query($sqlinsert) or die(mysql_error());
     mysql_query($sqlinsert) or die(mysql_error());

  ?>
  <br>
  <a href="administrador.php">

I have a problem with putting it back on the table. Will the Update part after I have to put all the other Table data?

It is already working. But I have a file problem. Because I always have to change its contents because it appears blank. For example:

AlvaraValidade<input type="date" name="AlvaraValidade" value="<?php echo $AlvaraValidade; ?>"><p>
AlvaraAnexo<input type="file" name="AlvaraAnexo" value="<?php echo $AlvaraAnexo; ?>"><p>

In this example the first one appears the old date and in File it does not appear. And then I have to re-enter an Attachment.

  • As I understand it, you can’t save the data, right? , is the database update correct? Try it: UPDATE tb_worker SET Name='$Name', Address = '$address', Email = '$Email' WHERE id = '$id'";

  • I have so now and do not give. A doubt. My table is: Id,Name,Address,Type,Email,Field,Field2... In the Update part I have to put all fields in the table?

  • no, you can just put the fields you want to update to

  • Now I put in a comment all fields where you can make change.

  • The question is extremely confusing as well as its "answers" that are not answers below?

  • In the form I have the data to change. And in the field $Alvaraanexo = $displays["Alvaraanexo"]; it doesn’t show me and only lets me put a new file

  • Detail that function mysql_query is no longer recommended for database access, due to the various gaps that result in SQL Injections, among other problems. Prefer use of PDO or Mysqli in place.

  • Remember to choose the best answer. You ask a lot of questions and those who answer like to get feedback as well. Hug.

Show 3 more comments

2 answers

8

Changes the sql of

 $sqlinsert = "Update tb_trabalhador
                         id,
                         Nome='$Nome',
                         Morada='$Morada',
                         Email='$Email'
                where id='$id' ";

for:

 $sqlinsert = "Update tb_trabalhador SET
                         Nome='$Nome' ,
                         Morada='$Morada',
                         Email='$Email' 
               where id='$id' ";

for which the set and remove the id as it will not be updated.

Send the id for input hidden

<form action="salvaralteracao.php" method="POST">
   <input type="hidden" name="id" value="<?php echo $id;?> " />

Remember that only one record should be changed in case your query needs one WHERE

include("conectar.php");
$id = filter_var($_GET['id'],  FILTER_VALIDATE_INT);
$sql = mysql_query("Select* From tb_trabalhador WHERE id = $id");

while($exibe = mysql_fetch_array($sql)){
   $id = $exibe["id"];
   $Nome = $exibe["Nome"];
   $Morada = $exibe["Morada"];
   $Email = $exibe["Email"];
}
  • : Undefined variable: id I have the variable. I even changed the POST if ( isset( $_POST['id'] ) ) { $id = $_POST['id']; }

  • In your form there is no field called id only nome, morada and email

  • But I’m just putting Id next to Where right now and it gives me the same problem.

  • I put the Id in the form? But in others I ask to change the data

  • @user3253195, yes you need a field id no form, na sql o id will not be updated.

  • You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'Alvaranumero='23423432',Alvaravalidade='2014-02-28',Alvaraanexo='telm__ext.html'' at line 1 Now I have this. has to do with the placement of?

  • You can post the code that generated this error?

  • SQL Injection fucked in $id=$_GET['id']; and mysql_query("Select* From tb_trabalhador WHERE id = $id");. There was a tear here

  • Boa, continued with a problem that is: It appears all right to change but when I save it does not replace what I had. Adds a new one just like the other but changed

Show 4 more comments

4

From what I understand you are wanting to reformulate this update form to make it work. Below is a way to do.

In this example only one file is used Altering.php, where it identifies if the request is an edit request or if it is the form request for editing.


Altering.php

<html>
<head>
    <title>Alterar</title>
</head>
<body>

<?php
// Inclui arquivo de conexão
include("conectar.php");

// Se for uma requisição POST, faz a ação de salvar as informações
if ($_SERVER['REQUEST_METHOD'] === 'POST') 
{
    $id = $_POST["id"];
    $Nome = $_POST['Nome'];
    $Morada = $_POST['Morada'];
    $Email = $_POST['Email'];

    $sqlinsert = "UPDATE tb_trabalhador SET Nome='{$Nome}', Morada='{$Morada}', Email='{$Email}' WHERE id='{$id}' ";
    mysql_query($sqlinsert) or die(mysql_error());
    ?>

    <br/>
    <a href="administrador.php"> Área Administrador </a>

    <?php
} 
else // Se não for uma requisição POST, mostra o formulário
{
    $id = $_GET['id'];
    $sql = mysql_query("SELECT * FROM tb_trabalhador WHERE id = {$id}");

    // Verifica se recebeu ao menos um resultado (o que se espera)
    if($exibe = mysql_fetch_array($sql))
    {
        // Se recebeu, faz a leitura dos dados
        $id = $exibe["id"];
        $Nome = $exibe["Nome"];
        $Morada = $exibe["Morada"];
        $Email = $exibe["Email"];

        // Imprime formulário pré carregado
        ?>
        <form action="Alterar.php" method="POST">
            <input type="hidden" name="id" value="<?php echo $id; ?>">
            Nome<input type="text" name="Nome" value="<?php echo $Nome; ?>">
            Morada<input type="text" name="Morada" value="<?php echo $Morada; ?>">
            Email <input type="text" name="Email" value="<?php echo $Email; ?>">
            <input type="submit" value="Guardar">
        </form>
        <?php
    }
    else // ID inválido
    {
        // Imprime alerta em javascript e faz o redirecionamento para alguma página
        ?>
        <script>
            alert("ID Inválido!");
            window.location = 'administrador.php';
        </script>
        <?php
    }
}
?>
</body>
</html>

That script would work? YES!

But it can still be improved, as he is extremely vulnerable to script injections.

Some tips to improve:

  • Validate client data with javascript or jQuery before form submission;
  • Validate data by receiving it on the PHP server before doing anything;
  • Try to make your connection to the database with PDO.

There are many other things that can be improved, but one step at a time.

  • Well guys, as our colleague’s answer was answered by Thiago. I came to leave a tip for him. Your script is vulnerable to SQL Injection. Search on the subject is a very important security measure in the development of an application. There are functions in PHP that handle this failure, such as mysql_real_escape_string. Ex: $name = mysql_real_escape_string($_POST["name"]);

  • 1

    At the end of the answer I come to quote about this vulnerability as well. The ideal is not to use any function mysql_..., because all have already been depreciated. As a good study for our colleague user3253195 (laughs) is the tip to upgrade and use an object-oriented and native persistence layer (PDO), with all necessary treatments to avoid these vulnerabilities (such as Prepared statement / parameterized statement).

  • 1

    Sorry, I didn’t see until this information.

  • The most important thing here is to contribute to learning and even if it is a depreciated function, it undoubtedly serves as a complement. I believe that most here have already used quite a lot of mysql_, mysqli_, until stopping in PDO for applications that need performance or using some ORM (Doctrine for example) to gain speed in development and ease in mapping. Abraço Hiago!

Browser other questions tagged

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