简体   繁体   中英

How do I store multiple similar data in a session array in PHP?

I would like to be able to submit multiple times and store the data in a session array to be accessed in other server pages. I'n not sure how to do it.

<form action="Lab5-2.php" method="post">
<table>
    <tr>
      <td align="center"><b>Customer Code</b></td>
      <td align="center"><b>Product Code</b></td>
      <td align="center"><b>Quantity</b></td>
    </tr>
    <tr>
      <td align="left"><input type="text" name="customer" required autocomplete="off"></td>
      <td align="right"><input type="text" name="product" required autocomplete="off"></td>
      <td align="right"><input type="text" name="quantity" required autocomplete="off"></td>
      <td align="right"><input type="submit" value="Submit" style="width: 80px"></td>
    </tr>
  </table>
</form>

In Lab5-2.php you need to store like that:

session_start();
$postvariable=$_POST['customer'];
$_SESSION['customer']=$postvariable;

Obv for prevent Sql Attack use that guide

For you question on comment you can do that:

session_start();
$postvariable=$_POST['customer'];
$_SESSION['customer']=$_SESSION['customer'].','.$postvariable;

So for example you have insert product value "pc", then update next time have "mouse", you will have "pc,mouse"

Try something like this

session_start();

$_SESSION['data'][] = $_POST;

This should store all the $_POST data inside the session.

Use print_r($_SESSION['data']); to check if the data is appending to the session. This should do exactly what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM