0

当用户登录到我的网站时,我创建了一个名为的会话,该会话$_SESSION['session_city_id']从 MySQL 收集 ID

我想知道是否可以从表单中更改它包含的数据。我创建了如下select_city所示的表单:

<form name='select_city' action='sessions.php' method='post' style='display:inline;'>
  <select onchange='this.form.submit()'>
  <option value='1'>The first value</option>
  <option value='2'>The second value</option>
  <option value='3'>The third value</option>
  </select>
 <noscript><input type="submit" value="Submit"></noscript>
</form>

我的sessions.php样子是这样的(我刚开始用 PHP 编程,所以不要因为我是一个完整的菜鸟而抨击我!)

<?php
session_start();
$url = htmlspecialchars($_SERVER['HTTP_REFERER']); //Find out where the user came from

if (isset($_POST['select_city'])){

    $_POST['select_city'] = $city_id;
    
    if (isset($_SESSION['session_city_id'])){
    unset($_SESSION['session_city_id']);
    }
    $_SESSION['session_city_id'] = $city_id;                
}
header ("Location: $url"); //Redirect back to the page the user came from
?>
4

2 回答 2

2
if (isset($_POST['select_city'])){
    $city_id = $_POST['select_city'];
    $_SESSION['session_city_id'] = $city_id;                
}

不需要unset,你替换了赋值。

于 2012-05-31T20:58:57.720 回答
1

您可以使用新值覆盖会话变量(无需取消设置)

if (isset($_POST['select_city'])){
    $_SESSION['session_city_id']=(int)$_POST['select_city']; // add a better check!
}

并且不要忘记您所在领域的名称

<select name="select_city" onchange='this.form.submit()'>
于 2012-05-31T21:02:04.243 回答