Donna
Donna

Reputation: 1

how can i insert multiple marks for multiple students mysql

I have a dynamic table(that prints names of some students from a specific group in DB ) and for each student i have a checkbox where i have to choose the mark.I need help about how can i insert multiple marks for multiple students in mysql????

The table on the database where i have to do the insertof marks is sth like this: id name mark

and the code of html and php

<form>
<table >
<thead >
    <tr>
      <th>Nr</th>
      <th>Name_Surname</th>
      <th>Mark</th>
    </tr>
</thead>

 <tbody>
<?php
 $query = "select student.name from student where student.group = '2' " ;   
      $run=mysql_query($query);
      $d=1;
      while ($row=mysql_fetch_assoc($run))
{
?>
<tr>
<th ><?php echo $d++; ?>
</th>
<td>
<input type="text" name= "name" value = '<?php echo $row["name"];?> '>
 </td>
 <td>
 <fieldset >
 <label for="mark"></label>
    <select name="mark" >
    <option>-</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    </select>
</fieldset>
</td> 
</tr>
<?php } ?> 
</tbody>
</table>
<button type="submit" name="save" >Printo</button>
</form>

Upvotes: 0

Views: 1232

Answers (1)

Shaun Hare
Shaun Hare

Reputation: 3871

Couple of points for your code

  1. Try and use mysqli_query (as mysql_query was deprecated and while back and has been removed in the latest version see http://www.php.net/manual/tr/function.mysql-query.php)

  2. To insert you need to submit the form to a page with a post method, sanitise the post values (look at filter functions http://php.net/manual/en/function.filter-input.php) then use mysqli_query again something like

    $insert = $mysqli->query("INSERT INTO marks(id name mark) VALUES ($studentId1, $name, $mark), ($studentId2, $name, $mark), ($studentId3, $name, $mark)");

    if($insert){
        //return total inserted records using mysqli_affected_rows
        print $mysqli->affected_rows .' marks added.<br />'; 
    }else{
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
    }
    

you can also use a loop to generate the insert statement

Upvotes: 2

Related Questions