I have this script. Everything works fine except that the first "if" condition does not evaluate as expected because it does not echo out the code that proves that it evaluated properly.I have a page(index.php) containing a form with post action from where the $_POST['pincode']
is coming from. So when if($pincode !== $_POST['pincode'])
evaluates to true, instead of header location to echo the error message and come back to index.php page, what happens is that it routes to my checkpin.php (this script) and stays there.
NB: $_POST['pincode']
is a number input type in HTML.
$_SESSION['pincode']= $_POST['pincode'];
$conn = new mysqli("localhost","user","pass",'db');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = $conn->query("SELECT pincodex, pinmatch FROM voters_reg WHERE pincodex = '{$_SESSION['pincode']}'");
$row_count = $sql->num_rows;
if ($row_count == 1)
{
while($row = $sql->fetch_assoc()){
$pinmatch = $row['pinmatch'];
$pincode = $row['pincodex'];
if($pincode !== $_POST['pincode']){
$_SESSION['error'] = "first error message";
header('Location: index.php');
exit();
} elseif ($pinmatch == $_POST['pincode']){
$_SESSION['error'] = "second error message";
header('Location: index.php');
exit();
} else {
$_SESSION['success'] = "success message";
header('Location: pinsuccess.php');
exit();
}
}
}
$conn->close();