-1

I'm creating an MCQ app using PHP and MySQL. when I upload an image to the database using the php form I get these error I think there is no any error in my code I'm saying this because I'm following youtube tutorial to do this I have done exactly as what is shown in the video tutorial.

Notice: Undefined index: fopt1 in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 211

Notice: Undefined index: fopt1 in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 214

Notice: Undefined index: fopt2 in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 217

Notice: Undefined index: fopt2 in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 220

Notice: Undefined index: fopt3 in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 222

Notice: Undefined index: fopt3 in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 225

Notice: Undefined index: fopt4 in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 227

Notice: Undefined index: fopt4 in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 230

Notice: Undefined index: fanswer in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 232

Notice: Undefined index: fanswer in C:\USBWebserver v8.6\root\online-quizz\online-quiz\admin\add_edit_questions.php on line 235

//Here is my code filename- add_edit_questions.php

<?php

include "header.php";
include "../connection.php";

$id = $_GET["id"];
$exam_category = '';
$res = mysqli_query($link, "select * from exam_category where id = $id");
while($row = mysqli_fetch_array($res))
{
    $exam_category = $row["category"];
}

?>
  <div class="breadcrumbs">
            <div class="col-sm-4">
                <div class="page-header float-left">
                    <div class="page-title">
                        <h1>Add Questions <?php echo "<font color='red'>" .$exam_category. "</font>"; ?></h1>
                    </div>
                </div>
            </div>

        </div>

        <div class="content mt-3">
            <div class="animated fadeIn">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card"> 
                            <div class="card-body">
                    <form action="" name="form1" method="POST" enctype="multipart/form-data ">
                            <div class="col-lg-6">


<div class="card-header"><strong>Add New Questions - Text</small></div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add a Question </label>
    <input type="text" name="question"  class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Add Question">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Option 1 </label>
    <input type="text" name="opt1"  class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Add Option 1">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Option 2 </label>
    <input type="text" name="opt2"  class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Add Option 2">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Option 3 </label>
    <input type="text" name="opt3"  class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Add Option 3">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Option 4 </label>
    <input type="text" name="opt4"  class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Add Option 4">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Answer  </label>
    <input type="text" name="answer"  class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Add Answer">
</div>



<button type="submit" name="submit1" class="btn btn-primary"> Add Question</button>

</div>

<!-- image questions-->

<div class="col-lg-6">

<div class="card-header"><strong>Add New Questions - Images</small></div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add a Question </label>
    <input type="text" name="fquestion"  class="form-control"  placeholder="Add Question">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Option 1 </label>
    <input type="file" name="fopt1"  class="form-control">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Option 2 </label>
    <input type="file" name="fopt2"  class="form-control">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Option 3 </label>
    <input type="file" name="fopt3"  class="form-control">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Option 4 </label>
    <input type="file" name="fopt4"  class="form-control">
</div>

<div class="form-group">
    <label for="exampleInputEmail1"> Add Answer  </label>
    <input type="file" name="fanswer"  class="form-control">
</div>



<button type="submit" name="submit2" class="btn btn-primary"> Add Question</button>



</div>
</form>



                            </div>

                        </div> <!-- .card -->
                    </div>
                    <!--/.col-->

                    </div>
                   </div><!-- .animated -->
                 </div><!-- .content -->
                </div><!-- /#right-panel -->


<?php

if(isset($_POST["submit1"]))
{
    $loop = 0;

        $count=0;
        $res = mysqli_query($link, "select * from questions where category='$exam_category' order by id asc")
        or die (mysqli_error($link));

        $count = mysqli_num_rows($res);

        if($count == 0)
        {

        }
        else
        {
            while($row = mysqli_fetch_array($res))
            {
                $loop = $loop +1;
                mysqli_query($link,"update questions set question_no= '$loop' where id= '$row[id]' ");
            }
        }

        $loop = $loop + 1;

        mysqli_query($link, "insert into questions values(NULL, '$loop', '$_POST[question]', '$_POST[opt1]', '$_POST[opt2]', '$_POST[opt3]', '$_POST[opt4]', '$_POST[answer]', '$exam_category')")
        or die(mysqli_error($link));

        ?>

    <script type="text/javascript">

    alert("question added successfully");
    window.location.href = window.location.href;


    </script>


        <?php

}

?>



<?php

if(isset($_POST["submit2"])) 
{
    $loop = 0;

        $count=0;
        $res = mysqli_query($link, "select * from questions where category='$exam_category' order by id asc")
        or die (mysqli_error($link));

        $count = mysqli_num_rows($res);

        if($count == 0)
        {

        }
        else
        {
            while($row = mysqli_fetch_array($res))
            {
                $loop = $loop +1;
                mysqli_query($link,"update questions set question_no= '$loop' where id= '$row[id]' ");
            }
        }

        $loop = $loop + 1;

        $tm = md5(time());

        $fnm1 = $_FILES["fopt1"]["name"];
        $dst1= "./opt_images/" .$tm.$fnm1;
        $dst_db1 = "opt_images/" .$tm.$fnm1;
        move_uploaded_file($_FILES["fopt1"]["tmp_name"],$dst1);


        $fnm2 = $_FILES["fopt2"]["name"];
        $dst2= "./opt_images/" .$tm.$fnm2;
        $dst_db2 = "opt_images/" .$tm.$fnm2;
        move_uploaded_file($_FILES["fopt2"]["tmp_name"],$dst2);

        $fnm3 = $_FILES["fopt3"]["name"];
        $dst3= "./opt_images/" .$tm.$fnm3;
        $dst_db3 = "opt_images/" .$tm.$fnm3;
        move_uploaded_file($_FILES["fopt3"]["tmp_name"],$dst3);

        $fnm4 = $_FILES["fopt4"]["name"];
        $dst4 = "./opt_images/" .$tm.$fnm4;
        $dst_db4 = "opt_images/" .$tm.$fnm4;
        move_uploaded_file($_FILES["fopt4"]["tmp_name"],$dst4);

        $fnm5 = $_FILES["fanswer"]["name"];
        $dst5 = "./opt_images/" .$tm.$fnm5;
        $dst_db5 = "opt_images/" .$tm.$fnm5;
        move_uploaded_file($_FILES["fanswer"]["tmp_name"],$dst5);




        mysqli_query($link, "insert into questions values(NULL, '$loop', '$_POST[fquestion]', '$dst_db1', '$dst_db2', '$dst_db3', '$dst_db4', '$dst_db5', '$exam_category')")


        ?>

    <script type="text/javascript">

    alert("question added successfully");
    window.location.href = window.location.href;


    </script>


        <?php

}

?>



<?php
include "footer.php";


?>
  • well your code is vulnerable to SQL injection and you do not check that the file data is available before attempting to assign to variables (ie: `isset( $_FILES['fopt1'] )` etc ) – Professor Abronsius Jun 13 '20 at 06:26
  • in the above code there is a discrepency between the number of opening versus closing `div` tags. There are 26 opening and 27 closing – Professor Abronsius Jun 13 '20 at 06:30
  • I'd suggest having tow forms - one for the text based questions and another for the images – Professor Abronsius Jun 13 '20 at 07:06
  • thankyou @RamRaider after removing one extra div tag I don't get any error but still, the image doesn't get uploaded, in the database I see the path opt_images/ but not the file name I uploaded – Ahnaf Ahamed Jun 13 '20 at 10:05
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Dharman Jun 13 '20 at 11:11
  • @Dharman I have a new problem now my old error is fixed checkout the 3rd comment that's my new problem thanks – Ahnaf Ahamed Jun 13 '20 at 11:26
  • If you have a new problem then please ask a new question – Dharman Jun 13 '20 at 11:27
  • @AhnafAhamed - the problem, I think, lies in the fact that you are using a single form but hoping to process separate pieces of the form according to which button is clicked. Both buttons are in the same form and both have values. Your logic starts with `if(isset($_POST["submit1"]))` so once that is complete it will reload the page using GET - and although not tested I would expect that clicking either button will cause this fork in the logic to be used rather than the second brance as you want. This is why I suggested 2 forms – Professor Abronsius Jun 13 '20 at 12:05

1 Answers1

0

Check about PHP configuration, same problem: PHP Undefined index error $_FILES?

Also, please check about:

    <form action="" method="post" id="form1">
        <input type="text" name="fname" form="form1">
    </form>

Never give up.