Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:56:19+00:00 2026-06-17T17:56:19+00:00

$answersql = INSERT INTO Penalty_Marks (PenaltyAnswer, PenaltyMarks, QuestionId) VALUES (?, ?, ?); if (!$insertanswer

  • 0
 $answersql = "INSERT INTO Penalty_Marks (PenaltyAnswer, PenaltyMarks, QuestionId) 
    VALUES (?, ?, ?)";

if (!$insertanswer = $mysqli->prepare($answersql)) {
    // Handle errors with prepare operation here
}

   $c = count($_POST['incorrect']);

    for($i = 0;  $i < $c; $i++ )
    {


$incorrect = $_POST['incorrect'][$i];
$answerMarks = $_POST['answerMarks'][$i];
$numQuestion = $_POST['numQuestion'][$i];

$insertanswer->bind_param('sii', $incorrect, $answerMarks, $numQuestion);
$insertanswer->execute();

}

The above insert is not inserting all the incorrect answers into the table, it is only inserting one incorrect answer per question. What am I doing wrong?

Below is the currect db table looks like:

PenaltyAnswerId PenaltyAnswer  PenaltyMarks  QuestionId
1               A              4             12
2               D              1             13

Below is what it should of been like:

PenaltyAnswerId PenaltyAnswer  PenaltyMarks  QuestionId
1               A              4             12
2               B              3             12
3               D              0             12
4               D              1             13
5               E              1             13

Below is form:

<form id="PenaltyMarks" action="insertpenaltymarks.php" method="post">

    <table id='penaltytbl'>
    <?php

    foreach($ques_ans as $questionId => $inc_ans)
    {
        $q_row_span = count($inc_ans);
        $row_count = 0;
        $inc_ans = array_values($inc_ans);

    ?>

    <tr class="questiontd">
    <td>
    <input type="hidden" name="numQuestion[]" value="<?php echo$questionId?>" />
    </td>

    <td>
    <input type="hidden" class="hiddenincorrect" name="incorrect[]" value="<?php echo$inc_ans[$row_count];?>">
    </td>

    <td>
    <input  name="answerMarks[]" type="text" data-type="qmark" value='0'  />
    </td>

    </tr>
        <?php
            //remaining incorrect answers in separate row (if any) follows here
        if($row_count < $q_row_span - 1) 
        {
            for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>     
                <tr>
                <td>
                <input type="hidden" class="hiddenincorrect" name="incorrect[]" value="<?php echo$inc_ans[$i];?>">
                </td>

                <td class="answermarkstd">
                <input  name="answerMarks[]" type="text" data-type="qmark" value='0'  />            
                </td>
                </tr>
        <?php
            }
        }
    }

    ?>
    </table>
<input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" />
    </form>

Below is jquery handling submit:

myClickHandler = function(e) {

       if (confirm("Are you sure you want to Proceed?" + "\n")) {
         $.ajax({
           url: "insertpenaltymarks.php",
           data: $("#PenaltyMarks").serialize(),
           async: false,
           type: "POST"
         });
         return true;
      } else {
        return false;
      }
    };

    $('#PenaltyMarks').submit(myClickHandler);

});

UPDATE:

Question Table:

QuestionId (PK auto)  (int 10)
QuestionNo  (int 4)
QuestionContent (varchar 5000)


Penalty_Marks Table;

PenaltyAnswerId (PK auto) (int 10)
PenaltyAnswer (Varchar 5)
PenaltyMarks (int 3)
QuestionId (FK)  (int 10) Reference to QuestionId in Question Table 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:56:21+00:00Added an answer on June 17, 2026 at 5:56 pm

    Updated

    With your current code, you can var_dump and found that the $_POST['numQuestion'] array may contains a fewer member than the $_POST['incorrect'][$i] array. I have updated my code too.

    <form id="PenaltyMarks" action="insertpenaltymarks.php" method="post">
    
    <table id='penaltytbl'>
    <?php
    
    foreach($ques_ans as $questionId => $inc_ans)
    {
        $q_row_span = count($inc_ans);
        $row_count = 0;
        $inc_ans = array_values($inc_ans);
    ?>
    
    <tr class="questiontd">
    <td>
    <input type="hidden" name="numQuestion[<?php echo $questionId; ?>]" value="<?php echo$questionId?>" />
    </td>
    
    <td>
    <input type="hidden" class="hiddenincorrect" name="incorrect[<?php echo $questionId; ?>][]" value="<?php echo$inc_ans[$row_count];?>">
    </td>
    
    <td>
    <input  name="answerMarks[<?php echo $questionId; ?>][]" type="text" data-type="qmark" value='0'  />
    </td>
    
    </tr>
        <?php
            //remaining incorrect answers in separate row (if any) follows here
        if($row_count < $q_row_span - 1) 
        {
            for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>     
                <tr>
                <td>
                <input type="hidden" class="hiddenincorrect" name="incorrect[<?php echo $questionId; ?>][]" value="<?php echo $inc_ans[$i];?>">
                </td>
    
                <td class="answermarkstd">
                <input  name="answerMarks[<?php echo $questionId; ?>][]" type="text" data-type="qmark" value='0' />            
                </td>
                </tr>
        <?php
            }
        }
    }
    
    ?>
    </table>
    <input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" />
    </form>
    

    The insertion script should be changed too 🙂

    foreach($_POST['incorrect'] as $questionId => $incorrectArray)
    {
        for($i = 0; $i < count($incorrectArray), $i++)
        {
            $incorrect = $_POST['incorrect'][$questionId][$i];
            $answerMarks = $_POST['answerMarks'][$questionId][$i];
            $numQuestion = $_POST['numQuestion'][$questionId];
    
            $insertanswer->bind_param('sii', $incorrect, $answerMarks, $numQuestion);
            $insertanswer->execute();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// query $sql = INSERT INTO tool (title,details) VALUES (:title,:details) ; $q = $conn->prepare($sql);
I am having trouble being able to insert QuestionId into the Answer Table. The
I had always used something similar to the following to achieve it: INSERT INTO
I am trying to insert data into the database but I am recieving this
I want to perform an insert into the database so that the database table
I am inserting values into a Question and Answer Table. Now the Question Table
I am receiving two errors in mysqli: Warning: mysqli_stmt::execute(): (HY000/2014): Commands out of sync;
I want to use a single query to insert into a table and if
I need to insert 800000 records into an MS Access table. I am using
I'm using ASP.NET/VB and I'm trying to insert a date and time into an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.