UPTADE in php, I’m not getting it

Asked

Viewed 60 times

0

I’m a beginner in php and I’m trying to do a crud... in the edit button I made a function in js to appear the inputs and redirect to edit.php? id=echo $Row['id']..

CRUD - STUDENTS

Pupil Note Gang
        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);

3 answers

1

Hello can be problem in sql mount, try so:

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

Put the script out of 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>
  • Not it :( , the problem is that it takes the last registered id, it even edits, but edits the last one

  • n worked :d.

0

Gustavo, from what I understand is missing right at the beginning of your code already declare that there will be the option of using the id. Already make the connection and id statement right 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'];
  • Or these actions: <a href="read_one.php? id=" . $line['id'] . " '> <a href="read_one.php? id=<? php $_SESSION['db_login']['id'] ; ?>

  • What error have you been presenting? But, try these actions I indicated.

0

Hey, Gustavo, what’s up? so... I would recommend you to upgrade this table function only with php and html, even for the fact that as I am a beginner, below has a code that can help you, just replicate.

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 using mysql_real_escape to avoid SQL Injection.

Browser other questions tagged

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