I have created a page with name edit.php
. I have moved on this page from action.php
using a edit button. I have successfully retrieved the values in the respective text boxes and other form items. I have problem that if by mistake this edit.php
page is refreshed all values are gone. What is other way to maintain the values? Though thing are going well if page is not refreshed. If session variable is created than how values are retrieved of both that is of session variable and from database?
What I have did with problem.. I have requested "albumid" on action.php
page..
session_start();
$aid = mysql_real_escape_string($_REQUEST['albumid']);
Now if action.php
page is requested through edit.php
page using edit button. than I have created a session variable. and destroyed it after successful update query.
if (isset($_POST["edit"])) {
$_SESSION["aid"]=$aid;
$result= mysql_query("SELECT * FROM table WHERE a_id =".$_SESSION["aid"]) or die(mysql_error());
$row=mysql_fetch_array($result); }
It means now session is created.. if page is refreshed than also session values remains and accordingly values are selected from this variable.
if($_POST[开发者_如何学运维'update']!="") {
Update query
session destroyed }
Than also my problem is not solved that is if page is refreshed before hitting update button I am loosing all values.
session variables are just data you put into the $_SESSION
superglobal. Accessing them is no different than accessing any other array, except that the session array is saved for you automatically. All you need to remember is to do a session_start()
before doing anything with the session.
$_SESSION['formfield1'] = $_POST['formfield1'];
$_SESSION['formfield2'] = $_POST['formfield2'];
etc...
<input type="text" name="formfield1" value="<?php echo htmlspecialchars($_SESSION['formfield1']) ?>" />
By default, PHP uses file-based sessions. To put it into a database, you'd have to write your own session handlers and point PHP to them using session_set_save_handler()
.
When you submit, save the values in appropriately named $_SESSION members. Then on your page, if you can't find the members in $_GET/$_POST, you can choose to look them up in $_SESSION. Or vice versa. Every time the user submits a form though, you should always update $_SESSION so that the values are the most current. (in case they backtrack, or resubmit, or whatnot).
session_start();
if (!empty($_POST)) {
$_SESSION['post'] = $_POST;
}
elseif (empty($_POST) && !empty($_SESSION['post'])) {
$_POST = $_SESSION['post'];
}
just don't forget to unset($_SESSION['post']);
when you're done with it.
If I have understood your question correctly, I think you need some variables from database as well as from the session.
Simply put the key of your database tuple (or keys in case of multiple values) along with other stuff. Page, upon loading will check for the session variables and when it finds the key, it can use it to retrieve the data from the database.
In your previous page, the code will look like this :
$_SESSION["player_key"] = 56;
$_SESSION["tournament_key"] = 100;
And current page, start processing like this :
<?php
session_start();
$player = $_SESSION["player_key"];
$tournament = $_SESSION["tournament_key"];
/*
* your database connection steps
*/
$query = "select * from player where pid=".$player;
$res = mysql_query($query);
/*
Now assign values for your forms/components here or anywhere in the page. You don't have to read anything from the querystring.
*/
?>
精彩评论