Php 从表向MySQL数据库中插入多行
我根据4个下拉标准从数据库中检索数据。将它们显示在表格中,并为每个学生的分数添加一列。我想在另一个表中填充每行分数旁边显示的所有记录。我已经尝试了我所知道的一切,查看Stackoverflow中的scenerios,但似乎不起作用。我需要帮助Php 从表向MySQL数据库中插入多行,php,mysql,mysqli,Php,Mysql,Mysqli,我根据4个下拉标准从数据库中检索数据。将它们显示在表格中,并为每个学生的分数添加一列。我想在另一个表中填充每行分数旁边显示的所有记录。我已经尝试了我所知道的一切,查看Stackoverflow中的scenerios,但似乎不起作用。我需要帮助 <form id="form1" name="form1" method="POST"> <fieldset id="Result"> <legend>Result Capture</legend>
<form id="form1" name="form1" method="POST">
<fieldset id="Result">
<legend>Result Capture</legend>
<table width="1110" border="0">
<tr>
<td width="236">Session:<span id="spryselect1">
<label for="SessionAss"></label>
<?php
include 'Connections/connection2.php';
$res3= mysqli_query($connection,"SELECT * FROM schsession ");
?>
<select name="SessionAssigned" id="SessionAssigned" class="sess" />
<option>Select</option>
<?php while($row3=mysqli_fetch_array($res3)){?>
<option value="<?php echo $row3["session"];?>"><?php echo $row3["session"];?></option>
<?php };?>
</select></td>
<td width="231">Term:<span id="spryselect4">
<label for="Term"></label>
<?php
include 'Connections/connection2.php';
$res4= mysqli_query($connection,"SELECT * FROM term_tab ");
?>
<select name="Term" id="Term" class="term" />
<option>Select</option>
<?php while($row4=mysqli_fetch_array($res4)){?>
<option value="<?php echo $row4["name"];?>"><?php echo $row4["name"];?></option>
<?php };?>
</select></td>
<td width="219">Class <span id="spryselect1">
<label for="class"></label>
<?php
include 'Connections/connection2.php';
$res= mysqli_query($connection,"SELECT * FROM class_tab ORDER BY name");
?>
<select name="class" id="class" class="class">
<option>Select</option>
<?php while($row2=mysqli_fetch_array($res)){?>
<option value="<?php echo $row2["name"];?>"><?php echo $row2["name"];?></option>
<?php };?>
</select>
</span></td>
<td width="220" class="sub" id="sub" name="sub">Subject:</td>
<td width="182"><input type="submit" name="RetBtn" id="RetBtn" value="Retrieve Data" /></td>
</tr>
</table></fieldset>
</form>
<form id="form2" name="form2" method="post" action="">
<fieldset><legend>Result Computation</legend>
<table width="840" border="0">
<tr>
<td width="82" height="18">SN</td>
<td width="245">Admission No</td>
<td width="143">Surname</td>
<td width="87">Firstname</td>
<td width="87">Class</td>
<td width="87">Session</td>
<td width="87">Term</td>
<td width="69">Subject</td>
<td width="98">CA(Score)</td>
</tr>
<?php
if(!isset($_POST["RetBtn"])){
$class="";$sess="";$term="";$sub="";
}else{
$class=$_POST["class"];
$sess=$_POST["SessionAssigned"];
$term=$_POST["Term"];
$sub= $_POST["sub"];
$get = "SELECT * FROM stdsubtrans_tab,admission_tab WHERE stdsubtrans_tab.AdmNo=admission_tab.AdmNo AND stdsubtrans_tab.class='$class'
AND stdsubtrans_tab.CSession='$sess' AND stdsubtrans_tab.Term='$term'";
$confirm = mysqli_query($connection,$get);
$c=0;
while($fetch=mysqli_fetch_array($confirm)){
$c++;
echo "<tr>";
echo "<td>".$c."</td>";
echo "<td>".$fetch["AdmNo"]."</td>";
echo "<td>".$fetch["Surname"]."</td>";
echo "<td>".$fetch["Firstname"]."</td>";
echo "<td>".$fetch["class"]."</td>";
echo "<td>".$fetch["CSession"]."</td>";
echo "<td>".$fetch["Term"]."</td>";
echo "<td>".$sub."</td>";
echo '<td><input type="text" name="score"></td>';
echo"</tr>";
//if score supplied , then click to save to database
if(isset($_POST["saveBtn"])){
$score= $_POST["score"];
$insert = "INSERT INTO result_tab(CSession,Term,Class,AdmNo,subject,score)VALUES ('".$fetch["CSession"]."','".$fetch["Term"]."',
'".$fetch["class"]."','".$fetch["AdmNo"]."','".$sub."','".$score."') ";
$succ = mysqli_query($connection,$insert);
}
}
}
?>
<tr>
<td><input type="submit" name="saveBtn" id="saveBtn" value="Save Score" /></td><td> <input type="reset" name="cancel" id="cancel" value="Cancel" /></td>
</tr>
</table>
</fieldset>
</form>
结果捕获
会议:
挑选
挑选
挑选
我找到了这个问题的答案:
只需将name字段声明为一个数组,并使用foreach循环将它们插入到DB中
while($fetch=mysqli_fetch_array($confirm)){
$c++;
echo "<tr>";
echo "<td>".$c."</td>";
echo"<td><input type='text' name='admNo[]' value='".$fetch["AdmNo"]."'></td>";
echo"<td><input type='text' name='sname[]' value='".$fetch["Surname"]."'></td>";
echo"<td><input type='text' name='fname[]' value='".$fetch["Firstname"]."'></td>";
echo"<td><input type='text' name='class[]' value='".$fetch["class"]."'></td>";
echo"<td><input type='text' name='SessionAssigned[]' value='".$fetch["CSession"]."'></td>";
echo"<td><input type='text' name='Term[]' value='".$fetch["Term"]."'></td>";
echo "<td><input type='text' name='sub[]' value='".$sub."'</td>";
echo "<td><input type='text' name='score[]'></td>";
echo "</tr>";
}
//if score is supplied , then click to save to database
}
if(isset($_POST["saveBtn"])){
foreach($_POST["admNo"] as $rec=> $value){
$cl = $_POST["class"][$rec];
$term = $_POST["Term"][$rec];
$ad = $_POST["admNo"][$rec];
$Csess = $_POST["SessionAssigned"][$rec];
$sub = $_POST["sub"][$rec];
$sc = $_POST["score"][$rec];
$insert = "INSERT INTO result_tab(CSession,Term,Class,AdmNo,subject,score)VALUES('$Csess','$term',
'$cl','$ad','$sub','$sc')";
$succ = mysqli_query($connection,$insert);
}
}
while($fetch=mysqli\u fetch\u数组($confirm)){
$c++;
回声“;
回显“$c.”;
回声“;
回声“;
回声“;
回声“;
回声“;
回声“;
echo“我不完全确定你想做什么,但是你看了“插入…选择…”了吗?那么你有什么具体问题?错误?你有错误吗?你做过任何调试工作吗?@Jayde:我正在尝试插入表上显示的所有记录(包括为每个学生输入的分数)如何将html表中显示的行插入到具有click@MikeBrant.它没有显示错误。我只是在数据库表中没有看到任何记录。据我所知,我已经对它进行了调试。在其他时间,如果我在关闭标记之前添加代码,它将插入:echo““如果我对isset($_POST[“saveBtn”])进行注释,但这不会捕获分数,这是代码的主要部分。