1

Currently my code outputs a name, the attendance stored currently in the SQL database, and a text box beside it, where the value for attendance can be changed.

As multiple names are being outputted I have a submit button at the end so that all of the values can be submitted at the same time.

In order to check that the new altered value is being submitted for each user, I have an echo statement indicated by (a).
Once the button has been submitted it should display the name and the new value for the attendance that has been submitted.
Once the submit button has been pressed however it says that there is a: Array to string conversion error on line (a).

I would appreciate any ideas anyone has as to how I can solve this problem (thank you in advance).

//Here I have my SQL Statement (it's long so I haven't included it)  
$rownumber = $sqlquery->num_rows;
while($row7 = mysqli_fetch_array($sqlquery, MYSQLI_ASSOC)){
  echo $row7['FirstName'] . ' ' . $row7['LastName'] . ' "'  . $row7['LessonAtt'] . '"' ;

  $firstnameLabel = $row7['FirstName'];
  $lastNameLabel = $row7['LastName'];       
  $attLabel = $row7['LessonAtt'];
  $lessonID = $row7['LessonID'];  

  $error = '';
  echo <<<_END
    <form method='post' action='homepageInstructor.php?view=$user'>$error
    <span class='fieldname'>
    <input type ="text"  name='attendance[]' value=$attLabel>
_END;
  echo '<br>';

  if (isset($_POST['attendance'])) 
  {
      $attLabel = $_POST['attendance'];
     (a) echo $firstnameLabel .  $attLabel  . ' ';
  }
}

This is the result when print_r($_POST['attendance']); is done

5
  • Can you provide us with your form's HTML Commented Mar 12, 2016 at 20:24
  • @MathieudeLorimier I have indicated on the code above where the form is for you Commented Mar 12, 2016 at 20:26
  • Where are your quotes around the echo "text here"; Commented Mar 12, 2016 at 20:29
  • Can you post the result of print_r($_POST['attendance']);? Commented Mar 12, 2016 at 20:30
  • @C.Liddell I have added it as a picture of the page for you Commented Mar 12, 2016 at 20:36

1 Answer 1

1

Because your form item has [] at the end of it's name, you will end up with an array :

<input type ="text"  name='attendance[]' value=$attLabel>

That beeing said, to access it's values you should do like so :

$_POST['attendance'][0] // First occurence of the attendance field
$_POST['attendance'][1] // Second occurence of the attendance field
...

Regards

Sign up to request clarification or add additional context in comments.

2 Comments

I amended my code so it is as follows: </br> if (isset($_POST['attendance'])) { echo 'Hello'; for ($index = 0; $count <= $rownumber; $index++){ $attLabel = $_POST['attendance'][$index]; echo $firstnameLabel . $attLabel . ' '; } } </p> However the nothing is being printed after the for loop has been accessed (I checked using various echo's). Do you have any idea why this might be?
Yes that looks better :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.