我最近才开始使用php,所以这可能是一个简单的问题。但是,我不能在这里找到答案。
我有一个数据库,我想用我的html页面填充内容。要从一个页面填充它,可以很好地工作并且没有问题。
但是,我有不同的变量来自不同的html页面,但应该插入同一个数据库和同一行(具有相同的ID)。
例如:
第1页
<form action="tomydatabase.php" method="post">
<input type="hidden" name="varA" id="varA" value="">
<input type="hidden" name="varB" id="varB" value="">
</form>
2页
<form action="tomydatabase.php" method="post">
<input type="hidden" name="varC" id="varC" value="">
<input type="hidden" name="varD" id="varD" value="">
</form>
这是我的php脚本的一部分:
$A = mysqli_real_escape_string($con, $_POST['varA']);
$B = mysqli_real_escape_string($con, $_POST['varB']);
$C = mysqli_real_escape_string($con, $_POST['varC']);
$D = mysqli_real_escape_string($con, $_POST['varD']);
$sql="INSERT INTO `table02` (`variableA`, `variableB`, `variableC`, `variableD`)
VALUES ('$A', '$B', '$C', '$D');
我想要实现的是第一个html页面中的变量A和B的值与第二个页面中的变量C和D放在同一行中。我怎么能这样做?
答案 0 :(得分:0)
// Start Session
session_start();
// Get value for $A: from POST (if is set) else from SESSION (if is set)
if( isset($_POST['varA'])
$A = $_POST['varA'];
elseif(isset($_SESSION['varA']))
$A = $_SESSION['varA'];
else
$A = '';
// Store last value for $A
$_SESSION['varA'] = $A;
// Get value for $B: from POST (if is set) else from SESSION (if is set)
if( isset($_POST['varB'])
$B = $_POST['varB'];
elseif(isset($_SESSION['varB']))
$B = $_SESSION['varB'];
else
$B = '';
// Store last value for $B
$_SESSION['varB'] = $B;
// Get value for $C and $D
$C = (isset($_POST['varC'])) ? $_POST['varC'] : '';
$D = (isset($_POST['varD'])) ? $_POST['varD'] : '';
// Make query
$sql = sprintf(
"INSERT INTO `table02` (`variableA`, `variableB`, `variableC`, `variableD`)
VALUES ('%s', '%s', '%s', '%s')",
mysqli_real_escape_string($con, $A),
mysqli_real_escape_string($con, $B),
mysqli_real_escape_string($con, $C),
mysqli_real_escape_string($con, $D)
);
答案 1 :(得分:0)
利用Session!
if (!isset($_SESSION)) {
session_start();
}
// Get Values
if(isset($_POST['varA']))
$_SESSION['varA'] = $_POST['varA'];
if(isset($_POST['varB']))
$_SESSION['varB'] = $_POST['varB'];
if(isset($_POST['varC']))
$_SESSION['varC'] = $_POST['varC'];
if(isset($_POST['varD']))
$_SESSION['varD'] = $_POST['varD'];
// If we have got all variables ready
if (
isset($_SESSION['varA'])
&& isset($_SESSION['varB'])
&& isset($_SESSION['varC'])
&& isset($_SESSION['varD'])
)
{
// Make query
$sql = sprintf(
"INSERT INTO `table02` (`variableA`, `variableB`, `variableC`, `variableD`)
VALUES ('%s', '%s', '%s', '%s')",
mysqli_real_escape_string($con, $_SESSION['varA']),
mysqli_real_escape_string($con, $_SESSION['varB']),
mysqli_real_escape_string($con, $_SESSION['varC']),
mysqli_real_escape_string($con, $_SESSION['varD'])
);
}