-2

I'm trying to send the id to another page

I tried $_SESSION but it just takes the last id and deletes

I'm using the Foreach function

$_SESSION['IDIt'] just takes the last ID and $_GET['IDIt'] doesn't work

<div class="container">
  <div class="table-responsive">
    <h4 class="text-center mt-4">Itenerario</h4>
    <table class="table table-hover">
      <thead>
        <tr>
          <th width="35%">Local</th>
          <th width="15%">Data</th>
          <th width="10%">Custo</th>
          <th width="10%">N Pessoa</th>
          <th width="25%">Opções</th>
        </tr>
      </thead>
      <tbody>
        <?php
          foreach($resultado as $registo){ ?>
          <tr>
            <td>
              <?php echo $registo["Local"] ?>
            </td>
            <td>
              <?php echo $registo["Data"] ?>
            </td>
            <td>
              <?php echo $registo["Custo"] ?>
            </td>
            <td>
              <?php echo $registo["Pessoa"] ?>
            </td>
            <td>

              <a class="btn btn-sm btn-danger" href="eliminar.php?id=<?php echo $registo[" IDIt "]; ?>">Eliminar</a>
              <a class="btn btn-sm btn-success" href="#">Editar</a>
            </td>
          </tr>


          <?php }  ?>
          <?php
            $_SESSION['IDIt'] = $registo["IDIt"];
          ?>
      </tbody>
    </table>
  </div>
</div>

Eliminar.php

<?php 
    session_start();
    include 'conexao.php';
    if (isset($_SESSION['IDIt'])) {
        $id = $_SESSION['IDIt'];

    //$query = ("DELETE * from reserva_it where IDIt = $id");
    $sql ="DELETE from reserva_it Where IDIt like '$id'";

    $result = mysqli_query($conexao, $sql);

    echo 'O Reserva com o id = '; echo $id; echo ' foi apagado';
    }else{
        echo 'erro';
    }
?>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Nelson
  • 1
  • 2
  • You need to use `session_start()` on every page, at the very top of the file. This would be better done using `$_GET` or `$_POST` elements though. You do realize that you can't set a variable in a loop without overwriting, yes? – miken32 May 27 '21 at 20:02
  • I already have on every page – Nelson May 27 '21 at 20:06
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman May 27 '21 at 20:10
  • so better dont use a loop? – Nelson May 27 '21 at 20:11
  • _I already have on every page_ Show your actual code then. Nobody can tell the issue when there's missing code. – AbraCadaver May 27 '21 at 20:25

1 Answers1

1

With session you can't assign a single id for every row of table, so you have to retrieve the id with GET:

if (isset($_GET['id'])) {
    $id = $_GET['id'];
    ...

The index of $_GET have to be the same that you use in href statement (you use eliminar?id=...) and it's why your $_GET['IDIt'] in eliminar.php don't works. You have to change it with the code show in my answer

Stefino76
  • 369
  • 4
  • 10
  • Warning: I have answer your question about send the ID value but you should never use get value directly in your SQL query. You should use PDO or try to sanitize your Get value. For example, if your IDIt col have only numerical value you can write $id = intval($_GET['id']) – Stefino76 May 27 '21 at 21:03
  • I've run the code and it works. You have write that your get statement was $_GET['IDIt']. That doesn't work because you have to write $_GET['id'] – Stefino76 May 30 '21 at 13:23