1

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> 

Here's my database

17
  • Can we see your form? Commented Mar 28, 2015 at 16:09
  • Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Also add or die(mysql_error()) to mysql_query(). Also use isset() inside and in conjunction with if($iden['correct_answer'] == $_REQUEST['ans']). You also don't have a submit button. Commented Mar 28, 2015 at 16:11
  • You're also using the wrong variable while($iden = mysql_fetch_array($myData)) <=== Commented Mar 28, 2015 at 16:16
  • @D4V1D The form is inside the PHP code Commented Mar 28, 2015 at 16:25
  • 1
    @Noobist To be honest, I've never had to build quiz codes before. However, you can have a look at this Q&A on Stack stackoverflow.com/q/12136151 which could give you some ideas as well as this one stackoverflow.com/q/10356623 - These were found after Googling "quiz mysql php" Commented Mar 28, 2015 at 18:18

1 Answer 1

2

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>';
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.