我在test1.php中有输入表单,我想检查数据库中的用户输入,如果用户输入在数据库中不存在,则进入test2.php并在test2.php中回显用户输入。否则,重定向回test1.php和echo,不可用。
现在,如果输入存在于sql中,我可以重定向回上一页但是在关闭连接后我无法在test2.php中回显用户输入。
这是test1.php
<!doctype HTML>
<html>
<head>
</head>
<?php
if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
echo '<ul class="err">';
foreach($_SESSION['ERRMSG_ARR'] as $msg) {
echo '<li>',$msg,'</li>';
}
echo '</ul>';
unset($_SESSION['ERRMSG_ARR']);
}
?>
<form id="loginForm" name="loginForm" method="post" action="test-exec.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th>Email </th>
<td><input name="box" type="text" class="textfield" id="box" /></td>
</tr>
<td><input type="submit" name="Submit" value="Check" /></td>
</tr>
</table>
</form>
这是test-exec.php
<?php
session_start();
require_once('db/config.php');
$errmsg_arr = array();
$errflag = false;
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if(!$con) {
die('Failed to connect to server: ' . mysqli_connect_error());
}
$db = mysqli_select_db($con, DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$box = mysqli_real_escape_string($con, $_POST['box']);
if($box != '') {
$qry = "SELECT * FROM members WHERE box='$box'";
$result = mysqli_query($con,$qry);
if($result) {
if(mysqli_num_rows($result) > 0) {
$errmsg_arr[] = 'Box already in use';
$errflag = true;
}
@mysql_free_result($result);
}
else {
die("Query failed");
}
}
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: test1.php");
exit();
}else {
header("location: test2.php");
exit();
mysqli_close($con);
}
?>
这里是test2.php
<!doctype HTML>
<html>
<head>
</head>
<form id="loginForm" name="loginForm" method="post" action="register.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<th>Email </th>
<td><input name="box" type="text" class="textfield" id="box" value="<?php echo $_POST['box']; ?>" /></td>
</tr>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
答案 0 :(得分:1)
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: test1.php");
exit();
}else {
$_SESSION['SESS_box'] = $box;
session_write_close();
header("location: test2.php");
exit();
mysqli_close($con);
}
?>
then in the html use this:
<?php echo $_SESSION["SESS_box"]; ?>
to echo the input.