-1

How to show all array data using php.

I make question base project. 2 question and each question 4 option. Separately Question table in database. Separately option table in database.

enter image description here

PHP Code:

<?php  
include 'main.php';
include 'config/conn.php';
$quizid = $_GET['q'];
$sql = "SELECT * from addquiz Where quizid = $quizid";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
     $qn = $row['No_ of_question'];
?>


    <h1 class="heading">Enter Question Details</h1>
    
<?php
    for ($i=1; $i <=$qn ; $i++) { 
        // echo $i;
?>
    <div class="question">
        <div class="addquestion">
            <form method="post" action="questionsend.php?q=<?php echo $qn?>">
                <input type="hidden" name="quizid" value="<?php echo $quizid; ?>">
    <textarea name="questiontext[]" placeholder="Enter Question No <?php echo $i?>"></textarea>
    <label><?php $row["quizid"] ?></label>

    <input type="text" name="optiona[]" placeholder="Enter option a">
    <input type="text" name="optionb[]" placeholder="Enter option b">
    <input type="text" name="optionc[]" placeholder="Enter option c">
    <input type="text" name="optiond[]" placeholder="Enter option d">
    <select name="correctoption[]">Select Option here
        <option disabled="">Select Option of Q. 1</option>
        <option>a</option>
        <option>b</option>
        <option>c</option>
        <option>d</option>

    </select>
    
    

    <?php   
    }
    ?>
<button name="submit">Submit</button>   
    </form>
    </div>
</div>
<?php
}
?>

sendquestion.php:

<?php
if(isset($_POST['submit'])){
$qn = $_GET['q'];
$quizid = $_POST['quizid'];
$textarea = $_POST['questiontext'];
$optiona = $_POST['optiona'];
$optionb = $_POST['optionb'];
$optionc = $_POST['optionc'];
$optiond = $_POST['optiond'];

$correctoption = $_POST['correctoption'];



for ($i=0; $i <$qn ; $i++) { 
        echo $quizid." ".$textarea[$i].'<br>';
        for ($j=0; $j <1 ; $j++) { 
            echo $quizid." ".$optiona[$j].'<br>';
            // echo $quizid." ".$optionb[$j].'<br>';
            // echo $quizid." ".$optionc[$j].'<br>';
            // echo $quizid." ".$optiond[$j].'<br>';
        }
}
}
?>

screen-shot:

enter image description here

enter image description here

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • It is unclear from the above what your question is or what issues you are facing. Please would you clarify?! – Professor Abronsius Sep 01 '23 at 07:02
  • 1
    (Slightly OT, but still: `addquiz` is a bad table name here to begin with. Why is this not just `quiz`? The rest is not `addquestion`, `addanswer` etc. either, despite the fact those those will also get "added" to the database at some point.) – CBroe Sep 01 '23 at 07:07
  • 1
    **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unparameterised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Sep 01 '23 at 08:51
  • https://phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the [mysqli documentation](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) and this: [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. If you learnt your current technique from a tutorial or book, please don't use that resource again. – ADyson Sep 01 '23 at 08:51
  • I agree it's unclear what your question / problem is. Which array are you referring to, exactly? What result did you expect to see, based on the contents of that array? Can you show us the contents of the array? You also have shown us some screenshots, but you haven't said anything about them, so we don't know what you want us to understand from those pictures. Remember we cannot read your mind. See [ask], and then [edit] your post. We'll be very happy to help you, if we can understand clearly what the problem is. Thanks. – ADyson Sep 01 '23 at 08:53

0 Answers0