0

Hi I am have to enter marks of 50 students through a single page

        <table border="1">
                                          <tr><td height="50">UserID </td><td>Name</td><td>Marks </td></tr>
                                          <?php

                                          include('db_connect.php');

                                          $bname=$_POST['bname'];

                                          $res = mysql_query("SELECT * FROM user WHERE batch='$bname'");
                                          while($data = mysql_fetch_array($res))
                                          {
                                          $uname=$data['uname'];
                                          $uid=$data['uid'];
                                          echo "<tr><td>$uid </td><td>$uname</td> <td> <input type='text' size=3 name='$uname'></td></tr>";
                                          }

                                          echo "<tr><td colspan='2'><input type='hidden' name='bname' value='$bname'></td></tr>";
                                           ?>
                                          </table>

This is how I am creating the form. Please suggest me how to proceed further. How to retrieve data on next page and how to store them in a database.

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by diafol
0

You don't need a hidden field, how about a mark array?

echo "<tr><td>$uid </td><td>$uname</td> <td> <input type='text' size=3 name='mark[$user_id]'></td></tr>";

This can then be picked up by $_POST['mark'] array:

Use a foreach loop to build a set of VALUES clause for the sql (via implode).

Then use an INSERT...UPDATE ON DUPLICATE KEY... statement

Will come back if stuck - have to shoot off now...

0

OK, back now...

if(isset($_POST['marks'])){
    //filter function for array_filter
    function filter_me($var){
         return preg_match('/^\d+$/',trim($var));
    }
    //grab all marks inputs
    $post = $_POST['marks'];
    //filter all inputs to leave integers only, including 0
    $filtered_marks = array_filter($post,'filter_me');
    //create value array
    if(!empty($filtered_marks)){
        //create value array
        foreach($filtered_marks as $k=>$v){
            $vals[] = "($k,$v)";    
        }
        //make a VALUES clause by joining all value items
        $valuestring = implode(",",$vals);
        //SQL to insert new marks or overwrite existing ones
        $str = "INSERT INTO marks (`user_id`,`mark`) VALUES $valuestring ON DUPLICATE KEY UPDATE `mark` = VALUES(`mark`)";
        echo $str;
    }else{
        echo "Nothing to update";   
    }
}

Then your form html/php:

   echo "<tr><td>$uid </td><td>$uname</td> <td> <input type='text' size=3 name='marks[$user_id]' /> [$mark]</td></tr>";

You need to extract the $mark and $user_id from the marks table - so a LEFT JOIN syntax may be required for your select query.

//EDIT
The [$mark] bit needs a little work so that it's not visible if a mark has not been previously recorded for a student.

That OK?

Edited by diafol: wvebe

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.