1

I want to load the data from database to a checkbox list to display and select and here is my code. But it's not working so can anyone correct this code for me?

When i execute this i get the error message as "Parse error: syntax error, unexpected $end in C:\xampp\htdocs\test.php on line 70"

<?php   
    $section = mysql_query("SELECT DISTINCT Section_Text FROM Section");

    while ($row = mysql_fetch_array($section)){
   {
    echo "<tr><td>";
    echo "<input type='checkbox' name='Section' value ='Section_Text'";
    echo " />";
    echo $row['Section_Text'];

    echo "</td></tr><br/>";
   }

?>
2
  • ThiefMaster is right, in order to avoid such mistaked in the future , try using a better worksapce - such as eclipse PDT or zend. Commented Jul 10, 2012 at 21:27
  • Whats with the <br/> tag ? You echo rows for a table, dont use <br/> divider beetween table rows. And please paste the netire file. Also, use distinct(field) Commented Jul 10, 2012 at 21:29

4 Answers 4

1

You have a { too much right after your while loop. Remove one of them and the code will work.

Besides that, you should really use PDO (or mysqli) instead of the deprecated mysql extension to access your database.

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

8 Comments

Which code line is 70 in your file ? put a note by this codeline , it will be helpful
I am not getting any error message now but the code is still not working. when i try to display numbers from an array in the checkbox list it works but when i try to display values from database it does not work.
try to run the query alone and see if there is a result
in this case, check your database table fields, thats where the error is
The query is working fine i tried the same by displaying in a dropdown box and its working.
|
1

In the while loop :

while ($row = mysql_fetch_array($section)){
   {

Why are there two {? Delete one of them.

Comments

1

Remove the double { after the while declaration.

I suggest you to refactor your code a bit.

  • Escape your output using htmlentities($row['Section_Text'])
  • Don't repeat yourself using positional $ printf notation
  • Use <label ... /> notation to keep your form accessible: check this

    <?php   
    $section = mysql_query("SELECT DISTINCT Section_Text FROM Section");
    
    $n = 0;
    while ($row = mysql_fetch_array($section))
    {
        print "<tr><td>" . PHP_EOL;
        printf('<input type="checkbox" name="section_%2$d" id="section_%2$d" value="%1$s" />
             <label for="section_%2$d">%1$s</label>' . PHP_EOL,
              htmlentities($row['Section_Text'], ENT_QUOTES), $n++
        );
        print "</td></tr>" . PHP_EOL;
    }
    

Comments

0

there are 2 { in the while, remove one. and also get rid of the
tags, use css to get the desired padding

Comments

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.