I am trying to configure something, that if a user correctly inserts the results of integer 'a' and integer 'b', an if clause will execute.
Here is the code:
function showPassword()
{
$a = rand(0, 30);
$b = rand(0, 10);
$c = $a + $b;
echo "<p class='registrationtext'>A=" . $a . "</p>";
echo "<p class='registrationtext'>B=" . $b . "</p>";
echo "<p class='registrationtext'>c=" . $c . "</p>";
$result = mysqli_real_escape_string($link, $_POST['result']);
$status = "";
if (isset($_POST['showpassword'])) {
if ($result != $c) {
$status = "<p class='websitetext'>Incorrect.</p>";
} else {
$status = "<p class='websitetext'>Correct.</p>";
}
}
return $status;
}
The problem is, that no matter what I write, I always get the result Incorrect.
**EDIT** Here is the form:
<?php ob_start() ?>
<p class="websitetext" style="text-decoration:underline;">Show password</p><br/><br/>
<?php echo showPassword() ?>
<form name='usernamechange' action='' method='post' accept-charset='UTF-8'>
<br/>
<p class="registrationtext">Summed result:</p><br/><input type='text' class="input" name='result' maxlength="50" style="width:50px; height:25px;"/><br/><br/><br/>
</p><br/>
<input type='submit' class="input" name='showpassword' value="Submit" style="width:100px; height:25px;"/>
</form>
<br/><br/>
<?php $content = ob_get_clean() ?>
<?php ob_start() ?>
Password
<?php $title = ob_get_clean() ?>
<?php require 'Templates/Websites/Layout/layout.html.php' ?>
mysqli_real_escape_string() first parameter must be a mysql resource
$result = mysqli_real_escape_string($myConn, $_POST['result']);
Every time you submit the form a new value is assigned to $a, $b, and $c so comparing the submitted result with the new one is returning false a solution may be to save the result in the session and then compare the user's value with. like:
if(isset($_SESSION['result'])==false){
$a = rand(0, 30);
$b = rand(0, 10);
$c = $a + $b;
$_SESSION['result']=$c;
}
echo "<p class='registrationtext'>A=" . $a . "</p>";
echo "<p class='registrationtext'>B=" . $b . "</p>";
echo "<p class='registrationtext'>c=" . $c . "</p>";
$result = mysqli_real_escape_string($_POST['result']);
$status = "";
if (isset($_POST['showpassword'])) {
if ($result != $_SESSION['result']) {
$status = "<p class='websitetext'>Incorrect.</p>";
} else {
$status = "<p class='websitetext'>Correct.</p>";
}
}