如何将PHP对象从一个页面传递到下一个页面两次?

问题描述:

$ID= $_GET['id'];

<form method="post" action="updateorder.php?id=?"<?php echo htmlspecialchars($ID);?>">

I am using PHP and I can pass through the ID from the index.php to this page (openorders). I would like to go from this (openorders.php) to another page to update my orders - updateorder.php. I have found how to pass it through once, but not one more time after that. How would you do this? Thank you.

  $ ID = $ _GET ['id']; 
 
&lt; form method =“post  “action =”updateorder.php?id =?“&lt;?php echo htmlspecialchars($ ID);?&gt;”&gt; 
  code>  pre> 
 
 

我正在使用PHP 我可以通过ID从index.php传递到这个页面(openorders)。我想从这个(openorders.php)到另一个页面来更新我的命令 - updateorder.php。我已经找到了如何通过它 通过一次,但不会再多一次。你会怎么做?谢谢。 p> div>

As stated, use sessions and as an example.

Sidenote: Consult my "Edit" further below about ?id=?" in your form.

  • Assign a session array to the GET array, then use the same session array for subsequent pages.

The variable names don't really matter, it's the session array that is important here.

First start with, and from the page where you will retrieve the GET array:

session_start();

if(!empty($_GET['id'])){
   $ID = $_GET['id'];
}

$_SESSION['id'] = $_GET['id'];

$var_page1 = $_SESSION['id'];
   echo "ID #: " . $var_page1;

then on the next page:

session_start();

if(isset($_SESSION['id'])){

   $var_page2 = $_SESSION['id'];
   echo "ID #: " . $var_page2;

}

and the subsequent page following that:

session_start();

if(isset($_SESSION['id'])){

   $var_page3 = $_SESSION['id'];
   echo "ID #: " . $var_page3;

}

References:


Sidenote: If your GET array will always be an integer, you can pass (int) to it for more protection.

$ID = (int)$_GET['id'];

Reference:


Edit: (something I noticed while testing) - I used 123 as an example, being:

$_GET['id'] = 123;
$ID = (int)$_GET['id'];

Your form will need to be adjusted because of ?id=?". (the extra ? and quote).

<form method="post" action="updateorder.php?id=?"<?php echo htmlspecialchars($ID);?>">
                                               ^^ extra question mark and quote - delete it

Because that will translate to the following (and in HTML source) which is a ? and adding an additional quote, which is something you don't want and will fail you.

<form method="post" action="updateorder.php?id=?"123">

It will need to be changed to the following:

<form method="post" action="updateorder.php?id=<?php echo htmlspecialchars($ID);?>">

which will produce the following in HTML source:

<form method="post" action="updateorder.php?id=123">

you can get id value inside updateorder.php with this code. it is just because you are sending data with post in openorders.php. that why you can not get data with $_GET global

if(!empty($_POST['id'])){
$id = $_POST['id'];
}else{
$id=0;
}