خرید بک لینک
Hi,

I am trying to compare two strings to see if they are the same but I'm unable to get the correct result as in out of the 4/5 choices, I've added to a question only 1 should be correct.:mad:

The strings are $correct_choice and $choice, I have looked at various forums and tried to get the statement to work but I either get all the questions as one or as zeros. I would be grateful if someone can inform me where I have gone wrong:)

PHP Code:

 <?php 
        session_start
(); 
        include 
'includes/dbh.inc.php';
        include_once 
'header.php'
         
                    if (isset(
$_POST['submit'])) { 
                     
                    
//get the post varibles 
                    
$question_number $_POST['question_number']; 
                    
$question_text $_POST['text']; 
                    
$correct_choice $POST['correct_choice'];
                    
                    
                    
//choice array 
                        
$choice = array();//$choice is the key and choice1 etc.. is the value 
                        
$choice[1] = $_POST['choice1'];
                        
$choice[2] = $_POST['choice2'];
                        
$choice[3] = $_POST['choice3'];
                        
$choice[4] = $_POST['choice4'];
                        
$choice[5] = $_POST['choice5'];
                       
                        
                        
//if the user clicks sumbit without entering a question an error is displayed otherwise the question is submitted.
                          
if ($question_text == '')
                          {
                       
$error 'ERROR: Please add question before clicking submit!';
                       echo 
$error;
                       }else
                       {
                        
//question query 
                    
$query "INSERT INTO `questions`(question_number,text) VALUES ($question_number,'$question_text')"
                    
//echo $query;
                        //run query 
                 
if (mysqli_query($co,$query)) 
                 {
    echo 
"A new question has been created successfully";
} else 
{
    echo 
"Error: " $query "<br>" mysqli_error($co);
}
}
        if (
$query)
        {
               foreach (
$choice as $choice => $value
               {  
                   if (
$value != '')
                   {
                         if (
$correct_choice == $choice){ 
                                         
$is_correct 1
                                     }else{ 
                                         
$is_correct 0
                                     } 
                                     
//choice query 
                                 
$query "INSERT INTO  `choices` (`question_number`,`is_correct`,`text`) VALUES  ($question_number,$is_correct,'$value')";
                                
                                      
//echo $query;
                                     
if (mysqli_query($co,$query)) {
    echo 
"New choices have been added successfully";
} else {
    echo 
"Error: " $query "<br>" mysqli_error($co);
}
               }}}}                             
                 
                     
                    
// get the total questions 
    
$query "SELECT * FROM `questions`"
    
//echo $query; 
     //get results from questions 
     
$questions $co -> query($query) or die($mysqli->error.__LINE__); 
     
$total $questions -> num_rows
     
$next $total ++; 
                
?>      
    <section class="main-container">
            <div class="main-wrapper">
                <h2>Quiz</h2>
                <?php
                    
if (isset($_SESSION['u_id'])) {
                        echo 
"You are logged in!";
                    }
                
?>
                <main>
                    <div class = "container">
                        <br>
                        <h1>Add a question</h1>
                        <br>

                        <form method = "POST" action = "test2.php">
                        <p>
                            <label> Question Number:</label>
                            <input type = "number" value = "<?php echo $total?>" name = "question_number"/>
                        </p>
                        <br>
                         <p>
                            <label> Question Text</label>
                            <input type = "text"  placeholder = "Enter the question here" name = "text"/>
                        </p>
                        <br>
                         <p>
                            <label>Choices #1:</label>
                            <input type = "text" placeholder = "Enter the first answer"name = "choice1"/>
                        </p>
                        <br>
                        <p>
                            <label>Choices #2:</label>
                            <input type = "text" placeholder = "Enter the second answer"name = "choice2"/>
                        </p>
                        <br>
                        <p>
                            <label>Choices #3:</label>
                            <input type = "text" placeholder = "Enter the third answer"name  = "choice3"/>
                        </p>
                        <br>
                        <p>
                            <label>Choices #4:</label>
                            <input type = "text" placeholder = "Enter the fourth answer"name = "choice4"/>
                        </p>
                        <br>
                        <p>
                            <label>Choices #5:</label>
                            <input type = "text" placeholder = "Enter the fifth answer" name = "choice5"/>
                        </p>
                        <br>
                        <p>
                            <label>Correct Choice Number:</label>
                            <input type = "number" name = "correct_choice"/>

                        </p>
                        <br>
                        <input  type = "submit" name = "submit" value = "submit" />
                        <br>
                        </form>

                    </div>
                </main>
            </section>
        <br>
        <?php
            
include_once 'footer.php';
        
?>

برچسب: نویسنده: آیلین رضوانی تاريخ: سه شنبه 14 شهريور 1396 ساعت: 18:46

صفحه بندی