【问题标题】:how can i insert multiple marks for multiple students mysql我如何为多个学生插入多个标记mysql
【发布时间】:2016-06-23 05:40:12
【问题描述】:

我有一个动态表(打印 DB 中特定组的一些学生的姓名),对于每个学生,我都有一个复选框,我必须在其中选择标记。我需要有关如何为多个插入多个标记的帮助mysql的同学????

我必须插入标记的数据库表是这样的: 身份证姓名标记

以及html和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>

【问题讨论】:

    标签: php html mysql sql


    【解决方案1】:

    为您的代码加分

    1. 尝试使用 mysqli_query(因为 mysql_query 已被弃用,而在最新版本中已被删除,请参阅 http://www.php.net/manual/tr/function.mysql-query.php

    2. 要插入,您需要使用 post 方法将表单提交到页面,清理 post 值(查看过滤器函数 http://php.net/manual/en/function.filter-input.php)然后再次使用 mysqli_query 类似

      $insert = $mysqli->query("INSERT INTO 标记(id 名称标记) 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);
      }
      

    你也可以使用循环来生成插入语句

    【讨论】:

    • 感谢您的回复!我在此类查询之前尝试过,但我的学生人数未知(这取决于他们所在的小组)。
    • 因此使用循环;您的帖子值将采用数组的形式,因此您可以遍历它们查看答案,例如stackoverflow.com/questions/14245588/…
    • 两者。查看准备好的语句
    • 是的,应该使用准备好的语句@Strawberry 完全同意
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多