Display only, but others cannot – Wong Juin Hao Feb 03 '16 at 12:16

  • @fusion3k Exactly, all the table data can be displayed inside ` – Wong Juin Hao Feb 03 '16 at 12:17
  • https://www.dropbox.com/s/k5bybke6k7hpuwl/Capture3.PNG?dl=0 Kindly have a look at my output.. – Wong Juin Hao Feb 03 '16 at 12:23
  • 2 Answers2

    3

    1) Remove <form> inside <table>. Because, A form is not allowed to be a child element of a table, tbody or tr. You can have an entire table inside a form. You can have a form inside a table cell. You cannot have part of a table inside a form. For more info, click this

    2) <option> don't have name attribute. Check here

    3) Give name to <select></select> . Check here

    4) Since, multiple values are being submitted to checkresult2.php page, you have to define name as name="subj[]" and name="grad[]" as array type.

    5) An Alternative way for showing data in rest 9 rows is : before while loop give that query there itself, like below.

    Updated Code.

    <form action="checkresult2.php">
        <table class="p1" bgcolor="#FFFFCC" bordercolor="#000000" align="center" width="771" border="2">
              <tr>
                  <td><div align="center"><strong>No.</strong></div></td>
                  <td><div align="center"><strong>Subject Name</strong></div></td>
                  <td><div align="center"><strong>Grade</strong></div></td>
              </tr>
    
                <?php 
              for($i=1; $i<=10; $i++)
              {?>
              <tr>
                  <td width="44"><div align="center"><?php echo $i; ?></div></td>
                  <td width="601">
                      <select name="subj[]">
                          <option value="">--- Please choose a subject ---</option>
                              <?php
                              $result = mysql_query("SELECT * FROM spm_subject");
                              while($s = mysql_fetch_assoc($result))
                              {?>
                                  <option value="<?php echo $s['name']; ?>"><?php echo $s["name"]; ?></option>
                          <?php } ?>
                      </select>
                  </td>
                  <td width="104"><div align="center">
                      <select name="grad[]">
                          <option value="">&nbsp;</option>
                              <?php
                              $result2 = mysql_query("SELECT * FROM spm_grade");
                              while($g = mysql_fetch_assoc($result2))
                              {?>
                                  <option value="<?php echo $g['grade']; ?>"><?php echo $g["grade"]; ?></option>
                          <?php } ?>
                      </select>
                  </div>
                  </td>
              </tr>
                <?php }?>
              <tr>
                  <td colspan="3">
                      <div align="center">
                          <input type="submit" value="Submit">
                      </div>
                  </td>
              </tr>
        </table>
    </form>
    

    [NOTE: mysql_ is deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Use MySQLi or PDO_MySQL extension should be used.]

    Community
    • 1
    • 1
    Nana Partykar
    • 10,556
    • 10
    • 48
    • 77
    0

    You have to preprocess the query populating two arrays:

    $names = $grades = array();
    for( $i=1; $i<=10; $i++ ) 
    {
        $names[] = mysql_fetch_assoc( $result );
        $grades[] = mysql_fetch_assoc( $result2 );
    }
    

    Then, in our code:

    <?php foreach( $names as $key => $name ) {?>
        <option name="subj"><?php echo $name['name']; ?></option>
    <?php } ?>
    

    and

    <?php foreach( $grades as $key => $grade ) {?>
        <option name="grad"><?php echo $grade['grade']; ?></option>
    <?php } ?>
    

    mysql_fetch_assoc fetch the current row from results and then ‘go’ to next row, so when the first for loop is terminate, the cursor has reached the end of results and - in next for loops, there is nothing to fetch.

    Please note:

    mysql_ syntax was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

    fusion3k
    • 11,568
    • 4
    • 25
    • 47