Reputation: 33
My if statement doesn't recognize the textbox name ("ans"). What should I change? and where should I put it? I also want to know if my if statement is correct; I want to compare the value inside the database with the input value ("ans"). Thanks in advance.
<?php
$con = mysql_connect("localhost", "root", "avtt123");
mysql_select_db("arvintarrega",$con);
$sql = "SELECT * from identification";
$myData = mysql_query($sql,$con);
echo '<form method = "POST">';
while($iden = mysql_fetch_array($myData))
{
echo '<center>' . $iden['question_number'] . '. ' . $iden['statement'] . ' <input type = "text" name = "ans"></input></center></br>';
if($iden['correct_answer'] == $_REQUEST['ans'])
{
echo "Correct";
}
else
{
echo "Wrong";
}
}
echo '</form>';
?>
<form method = "POST">
<input type = "submit" name = "submit" value = "Submit" class="btn btn-warning">
</form>
Upvotes: 1
Views: 2804
Reputation: 74217
Give the following a try which worked for me, and I'm using a mysqli_
connection and query, so change the xxx
with your own credentials.
<?php
$DB_HOST = 'xxx';
$DB_USER = 'xxx';
$DB_PASS = 'xxx';
$DB_NAME = 'xxx';
$Link = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($Link->connect_errno > 0) {
die('Connection failed [' . $Link->connect_error . ']');
}
$sql = "SELECT * from identification LIMIT 1";
$myData = mysqli_query($Link, $sql);
echo '<form method = "POST">';
while($iden = mysqli_fetch_array($myData))
{
echo '<center>' . $iden['question_number'] . '. ' . $iden['statement'] . ' <input type = "text" name = "ans"></center><br/>';
if(isset($_POST['submit']))
{
if(isset($_POST['ans']) && $iden['correct_answer'] == $_POST['ans']){
echo "Correct<br><br>";
}
else
{
echo "Wrong<br><br>";
}
} // if(isset($_POST['submit'])) end brace
} // while loop end brace
echo '<input type = "submit" name = "submit" value = "Submit" class="btn btn-warning">';
echo '</form>';
?>
Upvotes: 2