UPTADE in php, I'm not getting

0

I am a beginner in php and I am trying to do a CRUD ... in the edit button I did a function in js to appear the inputs and redirect to edit.php? id = echo $ row ['id']; However, it only takes the last registered user ..                        

CRUD - STUDENTS

                                                Student         Note         Class                  
        while($row = mysqli_fetch_assoc($listarAlunos)){
            $id = $row['id'];
            $nome = $row['nome'];
            $nota = $row['nota'];
            $turma = $row['turma'];
    ?>
        <td>
            <?php echo"$nome"?>
        </td>
        <td>
            <?php echo"$nota"?>
        </td>
        <td>
            <?php echo"$turma"?>
        </td>
        <td>
            <button onclick="abrirEditar()">Editar</button>

            <a href="remover.php?id=<?php echo $row['id'] ?>">
            <button>Remover</button>
            </a>
        </td>
        <tr></tr>

        <script type="text/javascript">
        function abrirEditar(){
            var div = document.getElementById("divEditar");
            div.innerHTML = "<br/><form method='post'>";
            div.innerHTML+= "Novo Nome:<input type='text' name='novoNome'/><br/>";
            div.innerHTML+= "Nova nota:<input type='text' name='novaNota'/><br/>";
            div.innerHTML+= "Nova Turma:<input type='text' name='novaTurma'/><br/>";
            div.innerHTML+= "<a href='editar.php?id=<?php echo $row['id'] ?>'><input type='submit' value='editar'/></a>";
            div.innerHTML+="</form>";
        }
        </script>
    <?php
        }
    ?>
    <div id="divEditar"></div>
    </table>
</body>

edit.php

include 'conexao.php';
$id = $_GET['id'];
$novoNome = isset($_POST['novoNome']) ? $_POST['novoNome'] : '';
$novaNota = isset($_POST['novaNota']) ? $_POST['novaNota'] : '';
$novaTurma = isset($_POST['novaTurma']) ? $_POST['novaTurma'] : '';

echo $id;
$sql = "UPTADE tbaluno SET nome = '$novoNome', nota = '$novaNota', turma = '$novaTurma' WHERE id = $id";

$editarAluno = mysqli_query($conexao, $sql);
    
asked by anonymous 05.12.2018 / 01:41

3 answers

1

Hello, there may be a problem in the sql assembly, try this:

$sql = "UPDATE tbaluno SET nome = '" . $novoNome . "', nota = '". $novaNota ."', turma = '" . $novaTurma . "' WHERE id = " . $id;

Put the script out of the while and as follows:

    <script type="text/javascript">
    function abrirEditar(id){
        var div = document.getElementById("divEditar");
        div.innerHTML = "<br/><form method='post'>";
        div.innerHTML+= "Novo Nome:<input type='text' name='novoNome'/><br/>";
        div.innerHTML+= "Nova nota:<input type='text' name='novaNota'/><br/>";
        div.innerHTML+= "Nova Turma:<input type='text' name='novaTurma'/><br/>";
        div.innerHTML+= "<a href='editar.php?id="+ id +">'><input type='submit' value='editar'/></a>";
        div.innerHTML+="</form>";
    }
    </script>

And edit it as follows:

  <button onclick="abrirEditar(<?php echo $row['id'] ?>)">Editar</button>
    
05.12.2018 / 03:05
0

Gustavo, from what I understand is missing at the beginning of your code already declare that there will be the option to use the id. Now make the connection and id declaration at the beginning, then whenever you use the id search, it will be the same. If you are using SESSION even better, you can declare this id search through SESSION. Example:

$id = $_SESSION['db_login']['id'];
    
05.12.2018 / 04:01
0

Hello, Gustavo, how are you? so I would recommend you to use this table function only with php and html, even for the fact that I am a beginner, below is a code that can help you, just reply.

Connection.php

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

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

mysqli_set_charset($conn, 'utf8');

USERS.php

<thead>
        <tr>
          <th>#</th>
          <th>Nome</th>
          <th>Nota</th>
          <th>Turma</th>
          <th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php
        $result = mysqli_query($conn, "SELECT * FROM table ORDER BY id DESC");
        while($row = mysqli_fetch_assoc($result)){ ?>
        <tr>
          <td><?php echo $row['id']; ?></td>
          <td><?php echo $row['nome']; ?></td>
          <td><?php echo $row['nota']; ?></td>
          <td><?php echo $row['turma']; ?></td>
          <td>
            <a href="edit.php?id=<?php echo $row['id']; ?>" class="btn btn-info btn-xs">
            <i class="fa fa-pencil"></i> Edit
            </a>
          </td>
        </tr>
      </tbody>
      <?php } ?>

Edit.php

if (isset($_POST['enviar'])) {

    $id      = $_POST['id'];
    $nota    = mysqli_real_escape_string($conn, $_POST['nome']);
    $nota   = mysqli_real_escape_string($conn, $_POST['nota']);
    $turma = mysqli_real_escape_string($conn, $_POST['turma']);

    $sql = "UPDATE 'users' SET 'nome' = '$nome','nota' = '$nota','turma' = '$turma' WHERE 'id' = '$id'";
    $ssl = mysqli_query($conn, $sql);

    header("Location: USER.php");
    exit();
}

I recommend that you use mysql_real_escape to avoid SQL Injection.

    
05.12.2018 / 12:08