0

I am trying to insert data into the database but I am recieving this error in my bind_param():

Fatal error: Only variables can be passed by reference in ... on line 48 

I have been searching on the net but I don't quite understand what this error means as out of al the inserts I have done this is first time I have recieved this error.

What is the problem which is causing this error?

Below is the insert code:

 $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++ )
    {


$insertanswer->bind_param('iii', $_POST['incorrect'][$i], $_POST['answerMarks'][$i], $_POST['numQuestion'][$i]);

Below is the form where it retrieves the details (this form below has been cut down for easier viewing):

<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>

    </form>
user1964964
  • 273
  • 2
  • 11

2 Answers2

1

The problem is that parameters to bindParam actually have to be variables, not strings or arrays. This code should work:

$incorrect = $_POST['incorrect'][$i];
$answerMarks = $_POST['answerMarks'][$i];
$numQuestion = $_POST['numQuestion'][$i];
$insertanswer->bind_param('iii', $incorrect, $answerMarks, $numQuestion);
Dean
  • 5,884
  • 2
  • 18
  • 24
  • Hi, sorry for lateness. Ok this has removed the error but I am getting this notice which is strange `Notice: Uninitialized string offset: 3 in ... on line 50 Notice: Uninitialized string offset: 4 in ... on line 50 ...` This is being displayed for this variable `$numQuestion = $_POST['numQuestion'][$i]; `. This is causing an incorrect insert for my question numbers. What does this mean. It does not appear for the other variables – user1964964 Jan 18 '13 at 11:24
  • It means that `$_POST['numQuestion']` is not actually an array, but a string. You have something else further up the chain, possibly in the form that is causing it. But given that this answered your initial question you should mark it as accepted :) – Dean Jan 18 '13 at 11:27
-2
Try this:
Declare variables x,y,z at appropriate locations.

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

    $x = $_POST['incorrect'][$i];
    $y = $_POST['answerMarks'][$i];
    $z = $_POST['numQuestion'][$i];

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

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

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


$insertanswer->bind_param('iii', x,y,z);


Hope that solves it.