简体   繁体   English

PHP 中的 PostgreSQL - 会话变量似乎在发布数据时自行取消设置

[英]PostgreSQL in PHP - session variable seems to unset itself when posting data

I'm trying to handle a form that updates some data in a postgreSQL table.我正在尝试处理更新 postgreSQL 表中某些数据的表单。 To do that, I GET the id for the item to update.为此,我获取要更新的项目的 ID。 The URL reads:网址如下:

[...]pojazd.php?id=1[...] , which successfully GETs the data. [...]pojazd.php?id=1[...] ,成功获取数据。

Then I want to store it in a session (that was opened before):然后我想将它存储在一个会话中(之前打开过):

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

When I use my $id variable to SELECT the data, it works fine.当我使用$id变量来选择数据时,它工作正常。

SELECT model, cena FROM pojazd WHERE id_poj = $id" does the job. SELECT model, cena FROM pojazd WHERE id_poj = $id"完成这项工作。

But when I want to update this data using a POST form:但是当我想使用 POST 表单更新此数据时:

if (isset($_POST['update'])){
[...]
UPDATE pojazd SET model = '$model', cena = '$cena' WHERE id_poj = $id;
}

nothing happens.没发生什么事。 When the $id variable is hardcoded (for example: replaced by 1 etc), it works fine.$id变量被硬编码时(例如:替换为 1 等),它工作正常。 But the $id variable seems to unset itself whenever the form is POSTed.但是,每当发布表单时, $id变量似乎都会自行取消设置。 I tried echo'ing the $id variable whilst disabling the redirecting, and that's exactly what happens - the $id variable disappears.我尝试在禁用重定向的同时回显$id变量,这正是发生的事情 - $id变量消失了。

I'm at a loss.我不知所措。 The clock is ticking, I really don't want to fail my class, but that problems ruins my entire project...时间在流逝,我真的不想让我的课不及格,但是这个问题毁了我的整个项目......

You need to replace你需要更换

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

with

if (isset($_GET['id'])) $_SESSION['id'] = $_GET['id'];

so that the value doesn't get overwritten with an empty value when the form is POST ed...这样当表单被POST ed 时,该值不会被空值覆盖...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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