Php 如何以多页形式存储会话信息并将信息发送到mySQL表
在我一直从事的一个项目中,我遇到的路障时间最长。我有一个多页表单,在这个表单中,我通过多个页面从最终用户那里收集数据,并在最后将它们提交到我的数据库中,但是当我检查表时,没有显示任何数据 第1页的表单如下所示Php 如何以多页形式存储会话信息并将信息发送到mySQL表,php,mysql,session,mysqli,Php,Mysql,Session,Mysqli,在我一直从事的一个项目中,我遇到的路障时间最长。我有一个多页表单,在这个表单中,我通过多个页面从最终用户那里收集数据,并在最后将它们提交到我的数据库中,但是当我检查表时,没有显示任何数据 第1页的表单如下所示 <?php session_start(); // If the user is not logged in redirect to the login page... if (!isset($_SESSION['loggedin'])) { header('Location
<?php
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
?>
<?php
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
//Fetching values from form 2
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
?>
<?php
code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
//Fetching values from form 3
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
extract($_SESSION['post']); // Function to extract array.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'qraccounts';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if ($stmt = $con->prepare('INSERT INTO logindata (database_value1, database_value2, database_value3, database_value4, database_value5) VALUES (?, ?, ?, ?, ?)')) {
$stmt->bind_param('sssss', $_POST['value1'], $_POST['value2'], $_POST['value3'], $_POST['value4'], $_POST['value5']);
$stmt->execute();
}
?>
表2看起来是这样的
<?php
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
//Fetching values from form 1
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
?>
中三看起来像
<?php
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
?>
<?php
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
//Fetching values from form 2
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
?>
<?php
code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
//Fetching values from form 3
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
extract($_SESSION['post']); // Function to extract array.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'qraccounts';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if ($stmt = $con->prepare('INSERT INTO logindata (database_value1, database_value2, database_value3, database_value4, database_value5) VALUES (?, ?, ?, ?, ?)')) {
$stmt->bind_param('sssss', $_POST['value1'], $_POST['value2'], $_POST['value3'], $_POST['value4'], $_POST['value5']);
$stmt->execute();
}
?>
完成表格看起来像
<?php
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
?>
<?php
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
//Fetching values from form 2
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
?>
<?php
code.
session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
header('Location: login.html');
exit();
}
//Fetching values from form 3
foreach ($_POST as $key => $value) {
$_SESSION['post'][$key] = $value;
}
extract($_SESSION['post']); // Function to extract array.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'qraccounts';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
if ($stmt = $con->prepare('INSERT INTO logindata (database_value1, database_value2, database_value3, database_value4, database_value5) VALUES (?, ?, ?, ?, ?)')) {
$stmt->bind_param('sssss', $_POST['value1'], $_POST['value2'], $_POST['value3'], $_POST['value4'], $_POST['value5']);
$stmt->execute();
}
?>
是否有任何东西会阻止我在多个会话中将用户数据存储在SQL表中
我的数据库是一个包含id、电子邮件、密码(哈希)、数据库值1、数据库值2、数据库值3、数据库值4、数据库值5的单表。我是否需要像在注册页面中那样存储前面的数据?看起来您已经在向会话传递值了。 您可以使用以下会话:
$_SESSION['database_value1'];
$_SESSION['database_value2'];
在数据库查询中,您应该使用
$\u SESSION['post']['value1']
而不是$\u post['value1']
。并用extract(…)
删除该行,因为它没有达到您期望的效果。如果我可以问一下,这个问题是怎么回事?下面有一个答案。你试过了吗?它解决了问题吗?