Data editing in PHP

Asked

Viewed 65 times

-3

Hello, I have a problem in my code that I did not find the problem.

When trying to edit the ID 1750 it runs but saved in ID 0, I wonder if my code has any error in this sense, why by clicking edit on the line I want it accesses and pulls data from it but when I try to save it always goes in ID 0.

Error showing
Warning: Trying to access array offset on value of type null in C: xampp htdocs eletrictel panel projects_edit.php online 244

// Classe Projeto Editar.php
<?php
include('../class/classe_verifica_login.php');
include_once('../class/classe_conexao.php');
$id = filter_input(INPUT_GET, "id", FILTER_SANITIZE_NUMBER_INT);
$result_empresa = "SELECT * FROM empresa WHERE id='$id' LIMIT 1";
$resultado_empresa = mysqli_query($conexao, $result_empresa);
$row_empresa = mysqli_fetch_assoc($resultado_empresa);
?>

<form method="POST" action="projeto_editar.php">
                  <div class="row">
                    <div class="col-md-5">
                      <div class="form-group">
                        <label class="bmd-label-floating">ID ( Desabilitado )</label>
              //linha 244          <input type="text" name="id" id="id" class="form-control" value="<?php echo $row_empresa['id']; ?>" disabled>
                      </div>
                    </div>
                    <div class="col-md-3">
                      <div class="form-group">
                        <label class="bmd-label-floating">Empresa</label>
                        <input name="nm_empresa" type="text" value="<?php echo $row_empresa['nm_empresa']; ?>" class="form-control">
                      </div>
                    </div>
                    <div class="col-md-4">
                      <div class="form-group">
                        <label class="bmd-label-floating">Status</label>
                        <input name="pj_status" type="text" value="<?php echo $row_empresa['pj_status']; ?>" class="form-control">
                      </div>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-6">
                      <div class="form-group">
                        <label class="bmd-label-floating">Nome do projeto</label>
                        <input name="nm_projeto" type="text" value="<?php echo $row_empresa['nm_projeto']; ?>" class="form-control">
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="form-group">
                        <label class="bmd-label-floating">Número de EI</label>
                        <input name="nm_ei" type="text" value="<?php echo $row_empresa['nm_ei']; ?>" class="form-control">
                      </div>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-12">
                      <div class="form-group">
                        <label class="bmd-label-floating">Nome da concessionária</label>
                        <input name="nm_concessionaria" type="text" value="<?php echo $row_empresa['nm_concessionaria']; ?>" class="form-control">
                      </div>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-md-4">
                      <div class="form-group">
                        <label class="bmd-label-floating">Cidade</label>
                        <input name="nm_cidade" type="text" value="<?php echo $row_empresa['nm_cidade']; ?>" class="form-control">
                      </div>
                    </div>
                    <div class="col-md-4">
                      <div class="form-group">
                        <label class="bmd-label-floating">Kilometragem</label>
                        <input name="qnt_kilometragem" type="text" value="<?php echo $row_empresa['qnt_kilometragem']; ?>" class="form-control">
                      </div>
                    </div>
                    <div class="col-md-4">
                      <div class="form-group">
                        <label class="bmd-label-floating">Quantidade de postes</label>
                        <input name="qnt_postes" type="text" value="<?php echo $row_empresa['qnt_postes']; ?>" class="form-control">
                      </div>
                    </div>
                    <!--
                      <div class="col-md-4">
                        <div class="form-group">
                          <label for="cadastro" class="bmd-label-floating">Cadastro</label>
                          <input name="cadastro" type="text" class="form-control">
                        </div>
                      </div>
                      -->
                  </div>
<button type="submit" class="btn btn-primary pull-right">Atualizar</button>

```

```
// Classe Projeto Editar.php

<?php
include('../class/classe_verifica_login.php');
include_once('../class/classe_conexao.php');

$id = filter_input(INPUT_POST, "id", FILTER_SANITIZE_NUMBER_INT);
$nm_empresa = filter_input(INPUT_POST, 'nm_empresa', FILTER_SANITIZE_STRING);
$pj_status = filter_input(INPUT_POST, 'pj_status', FILTER_SANITIZE_STRING);
$nm_projeto = filter_input(INPUT_POST, 'nm_projeto', FILTER_SANITIZE_STRING);
$nm_ei = filter_input(INPUT_POST, 'nm_ei', FILTER_SANITIZE_STRING);
$nm_concessionaria = filter_input(INPUT_POST, 'nm_concessionaria', FILTER_SANITIZE_STRING);
$nm_cidade = filter_input(INPUT_POST, 'nm_cidade', FILTER_SANITIZE_STRING);
$qnt_kilometragem = filter_input(INPUT_POST, 'qnt_kilometragem', FILTER_SANITIZE_STRING);
$qnt_postes = filter_input(INPUT_POST, 'qnt_postes', FILTER_SANITIZE_STRING);

$conexao->query("UPDATE empresa SET 
                nm_empresa = '$nm_empresa', 
                pj_status = '$pj_status', 
                nm_projeto = '$nm_projeto', 
                nm_projeto = '$nm_projeto', 
                nm_ei = '$nm_ei', 
                nm_concessionaria = '$nm_concessionaria', 
                nm_cidade = '$nm_cidade', 
                qnt_kilometragem = '$qnt_kilometragem', 
                qnt_postes = '$qnt_postes', 
                modificado = NOW()
                WHERE users.id = $id");

if(mysqli_affected_rows($conexao)) {
    $_SESSION['sucesso'] = true;
    header("Location: projetos_editar.php?id=$id");
} else {
    $_SESSION['negado'] = true;
    header("Location: projetos_editar.php?id=$id");
}
?>
```

  • I couldn’t reproduce the error. I removed includes because I have no way to replicate them, I removed the part of the database because I have no way to replicate, enabled the input and what was passed through the request post arrives unchanged.

2 answers

-2


Hello, have you checked the closing of the tag " "? I wonder why I’ve had the same problem and there is principle I have not found the closing of the tag in your code.

  • Good morning, thanks for the reply. I will test when arriving home and return here.

  • Good morning Samuel, I’ve managed to pull my system to my work I’m seeing here and I haven’t found this tag you refer to.

  • Is that did not appear the name of the tag more would be the <form></form> and <input type="text" name="id" id="id" class="form-control" value="<?php echo &#xA; $row_empresa['id']; ?>" disabled> change to <input type="hiden" name="id" id="id" class="form-control" value="<?php echo &#xA; $row_empresa['id']; ?>"> disable and fieldset usually do not let capture values

  • Fix... set the field as hidden <input type="hidden" name="id" id="id" class="form-control" value="<?php echo $row_empresa['id']; ?>"> disable and fieldset do not capture values

-2

The error in my view lies in that part of the code

<div class="col-md-5">
 <div class="form-group">
  <label class="bmd-label-floating">ID ( Desabilitado )</label>
  <input type="text" name="id" id="id" class="form-control" value="<?php echo 
  $row_empresa['id']; ?>" disabled>
 </div>
</div>

As far as I know disabled forms are not sent in the post, what Voce can use is the tag type="Hidden" instead of disabled, however it will not appear on the screen

  • Could be, in my test I enabled the input and so could not reproduce the error. Now that I tested with it disabled the error was played.

  • Good morning, thanks for the reply. I will test when arriving home and return here.

  • Good morning Augusto Vasques, I did the test a little and still continues to result error. What I noticed best at the moment and when I click update it redirects me to the link projects_edit.php? id= but without the ID number of the column I am editing.

Browser other questions tagged

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