I have input form in test1.php and I want to check user input in database, if the user input does not exist in database, it proceeds to test2.php and echo the user input in test2.php. else, redirects back to test1.php and echo, not available.
Now, I am able to redirect back to the previous page if the input exist in sql but I am not able to echo the user input in test2.php after closing connection.
Here is 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>
Here is 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);
}
?>
And here is 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>