我必须通过一次提交在数据库中的不同单元格中提交所有值 当我点击提交时,它只插入最后一个学生和最后一个主题标记 例子(退缩和物理):
<tbody>
<?php
$query="SELECT *FROM `students` WHERE `standard`='".$standard."'";
$ex_query=mysql_query($query);
while($row=mysql_fetch_array($ex_query)) {
$id=$row['id'];
$fname=$row['fname'];
$lname=$row['lname'];
$rollno = $row['rollno'];
$standard = $row['standard'];
$section = $row['section'];
?>
<tr>
<th scope="row">1</th>
<td><?php echo $fname; ?> <?php echo $lname; ?>
<input type="hidden" name="name" value="<?php echo $fname; ?> <?php echo $lname; ?>">
</td>
<td><?php echo $standard; ?>
<input type="hidden" name="standard" value="<?php echo $standard; ?>">
</td>
<td><?php echo $section; ?>
<input type="hidden" name="section" value="<?php echo $section; ?>">
</td>
<?php $querysub="SELECT *FROM `subject`";
$ex_querysub=mysql_query($querysub);
while($row=mysql_fetch_array($ex_querysub))
{
$id=$row['id'];
$subject=$row['subject'];
?>
<td><input type="hidden" name="subject" value="<?php echo $subject; ?>"><input type="text" class="form-control col-md-7 col-xs-12" name="marks"></td>
<?php } ?>
<!-- <td><a href="javascript:void(0)" onClick="edit(<?php echo $id ?>)"><i class="fa fa-edit"></i></a></td>
<td><a href="javascript:void(0)" onClick="deldata(<?php echo $id ?>)"><i class="fa fa-trash-o"></i></a></td> -->
</tr>
<?php }
?>
</tbody>
</table>
<input type="submit" name="submit">
<?php
if(isset($_POST['submit'])) {
$id
$name = $_POST['name'];
$standard = $_POST['standard'];
$section = $_POST['section'];
$subject = $_POST['subject'];
$marks = $_POST['marks'];
$query="INSERT INTO `marks` (`id` ,`name` ,`standard` ,`section` ,`subject` ,`marks`) VALUES (NULL, '$name', '$standard', '$section', '$subject', '$marks')";
$ex_query=mysql_query($query); ?>
<!-- <script>
window.location.href="view-standard.php";
</script> --> <?php
}
}
?>
</form>
答案 0 :(得分:0)
您可以为每个元素名称添加行索引,例如
<tr>
<th scope="row">1
</th>
<td>
<?php echo $fname; ?>
<?php echo $lname; ?>
<input type="hidden" name="name[]" value="<?php echo $fname; ?> <?php echo $lname; ?>">
</td>
<td>
<?php echo $standard; ?>
<input type="hidden" name="standard[]" value="<?php echo $standard; ?>">
</td>
<td>
<?php echo $section; ?>
<input type="hidden" name="section[]" value="<?php echo $section; ?>">
</td>
<?php $querysub="SELECT *FROM `subject`";
$ex_querysub=mysql_query($querysub);
while($row=mysql_fetch_array($ex_querysub))
{
$id=$row['id'];
$subject=$row['subject'];
?>
<td>
<input type="hidden" name="subject[]" value="<?php echo $subject; ?>">
<input type="text" class="form-control col-md-7 col-xs-12" name="marks">
</td>
<?php } ?>
<!-- <td><a href="javascript:void(0)" onClick="edit(<?php echo $id ?>)"><i class="fa fa-edit"></i></a></td>
<td><a href="javascript:void(0)" onClick="deldata(<?php echo $id ?>)"><i class="fa fa-trash-o"></i></a></td> -->
</tr>
然后,当您访问$_POST['name']
时,您将获得一个包含所有值的数组,您可以使用循环并将它们插入数据库中。
答案 1 :(得分:0)
$i=0;
while ($row = mysql_fetch_assoc($ex_querysub)) {
?>
<td>
<input type="text" name="form[<?php echo $i; ?>]['name']" value="<?php echo $row['name']; ?>" />
</td>
<td>
<input type="text" name="form[<?php echo $i; ?>]['section']" value="<?php echo $row['section']; ?>" />
</td>
.... you got the idea
<?php
$i++;
}
始终使用
mysql_fetch_assoc
代替mysql_fetch_array
。
行动本身
<?php
if (isset($_POST['form'])) {
foreach ($_POST['form'] as $row) {
$sql = sprintf("INSERT INTO marks SET
name = '%s',
section = '%s'
... add all fields here
",
strip_tags($row['name']), // you should always sanitize your input from user. Or you'll have SQL Injection problems
strip_tags($row['section']),
);
mysql_query($sql);
}
}
我更喜欢使用第二种形式的INSERT语句http://dev.mysql.com/doc/refman/5.7/en/insert.html。它与UPDATE相似,更容易理解。特别是如果您有大插入查询。通常应该在插入
上跳过主键auto_increment