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.

Recommended Answers

All 2 Replies

Member Avatar for diafol

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

Member Avatar for diafol

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.