Php does not assign value in $_SESSION

Asked

Viewed 344 times

0

This registration system, I have a button to delete the product in BD

<form action="remove-produto.php" method="post">
    <input type="hidden" name="id" value="<?=$produto['id']?>">
    <button class="btn btn-danger">remover</button>
</form>

when it goes to remove product it performs this:

 $id = $_POST['id'];
 removeProduto($conexao, $id);
 $_SESSION["success"] = "Produto removido com sucesso";
 header("location:produto-lista.php");
 die();

It deletes normally and returns the perfectly to the product-list.php that there’s that stretch on hold

<?php if(isset($_SESSION["success"])) { ?>
    <p class="alert-success"><?= $_SESSION["success"]?></p>
<?php  } ?>

I’ve tried to add the SESSION_START(), already on another page, the login page. I saw with the var_dump and says that $_SESSION["success"] is null.

PHP Version 5.6.15

Xampp 3.2.2

The Session is enabled and global variables also.

  • 3

    On every page that occurs session, you need to use session_start() as first statement. Try adding it on both pages.

2 answers

3


Before you assign this:

$_SESSION["success"] = "Produto removido com sucesso";

Put a session_start() and when to print as well, because you have to use the session_start() on all pages where you need to use session.

  • 1

    I didn’t know that, I thought I only needed 1 place, thanks!!! Solved 100%

1

The correct order to record in PHP session is:

session_start();
$_SESSION["success"] = "Produto removido com sucesso";
session_write_close();

The last command is used to save and close the Session.

If you want to read the session, consider:

session_start();
echo $_SESSION["success"];

Browser other questions tagged

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