如何对每个表单提交 $id + 1 ?
how to do $id + 1 with every form submit?
我想添加动态会话变量。所以我从 id=0 开始,但在我提交表单后,必须将 id 设置为 1,然后将其设置为 2,等等。下面是我尝试过的。我尝试在 if 提交函数中执行 $id++,但它不起作用。
<?php
$id = 0;
if (isset($_POST['submit'])) {
$_SESSION['person'][$id] = array(
'id' => $id,
'voornaam' => $_POST['firstname'],
'achternaam' => $_POST['lastname'],
'leeftijd' => $_POST['age'],
'rol' => $_POST['role'],
'omschrijving' => $_POST['description'],
);
$id++;
header('Location: mysite');
}
?>
$id = count($_SESSION['person']);
(假设你在别处定义了$_SESSION['person']
为数组。)
完整的片段如下所示:
if (!is_array($_SESSION['person']))
{
$_SESSION['person'] = array();
}
if (isset($_POST['submit']))
{
$id = count($_SESSION['person']);
$_SESSION['person'][$id] = array(
'id' => $id,
'voornaam' => $_POST['firstname'],
'achternaam' => $_POST['lastname'],
'leeftijd' => $_POST['age'],
'rol' => $_POST['role'],
'omschrijving' => $_POST['description'],
);
header('Location: mysite');
}
您实际上并没有在任何地方保留该值。所以每次都会重置为0。
您每次都创造价值:
$id = 0;
然后你增加它:
$id++;
但你不会把它放在任何地方。如果该值应该跟随用户的会话,请将其保留在会话中。类似于:
// get the id from session, or create a new one
$id = 0;
if (isset($_SESSION['id'])) {
$id = $_SESSION['id'];
}
// use the id value in your code
// increment the id and store it back in the session
$_SESSION['id'] = $id + 1;
没听清楚你的意思,但这可能会有所帮助
<?php
$id = 0;
if (isset($_POST['submit'])) {
$currID = $_SESSION['person']['id'];
$_SESSION['person'] = array(
'id' => $currID++,
'voornaam' => $_POST['firstname'],
'achternaam' => $_POST['lastname'],
'leeftijd' => $_POST['age'],
'rol' => $_POST['role'],
'omschrijving' => $_POST['description'],
);
header('Location: mysite');
}
?>
我想添加动态会话变量。所以我从 id=0 开始,但在我提交表单后,必须将 id 设置为 1,然后将其设置为 2,等等。下面是我尝试过的。我尝试在 if 提交函数中执行 $id++,但它不起作用。
<?php
$id = 0;
if (isset($_POST['submit'])) {
$_SESSION['person'][$id] = array(
'id' => $id,
'voornaam' => $_POST['firstname'],
'achternaam' => $_POST['lastname'],
'leeftijd' => $_POST['age'],
'rol' => $_POST['role'],
'omschrijving' => $_POST['description'],
);
$id++;
header('Location: mysite');
}
?>
$id = count($_SESSION['person']);
(假设你在别处定义了$_SESSION['person']
为数组。)
完整的片段如下所示:
if (!is_array($_SESSION['person']))
{
$_SESSION['person'] = array();
}
if (isset($_POST['submit']))
{
$id = count($_SESSION['person']);
$_SESSION['person'][$id] = array(
'id' => $id,
'voornaam' => $_POST['firstname'],
'achternaam' => $_POST['lastname'],
'leeftijd' => $_POST['age'],
'rol' => $_POST['role'],
'omschrijving' => $_POST['description'],
);
header('Location: mysite');
}
您实际上并没有在任何地方保留该值。所以每次都会重置为0。
您每次都创造价值:
$id = 0;
然后你增加它:
$id++;
但你不会把它放在任何地方。如果该值应该跟随用户的会话,请将其保留在会话中。类似于:
// get the id from session, or create a new one
$id = 0;
if (isset($_SESSION['id'])) {
$id = $_SESSION['id'];
}
// use the id value in your code
// increment the id and store it back in the session
$_SESSION['id'] = $id + 1;
没听清楚你的意思,但这可能会有所帮助
<?php
$id = 0;
if (isset($_POST['submit'])) {
$currID = $_SESSION['person']['id'];
$_SESSION['person'] = array(
'id' => $currID++,
'voornaam' => $_POST['firstname'],
'achternaam' => $_POST['lastname'],
'leeftijd' => $_POST['age'],
'rol' => $_POST['role'],
'omschrijving' => $_POST['description'],
);
header('Location: mysite');
}
?>