我使用复选框动态创建表行。检查其中的一些,然后执行并更新所选的查询。但我遇到的问题是,即使我使用了foreach循环,也只有第一个选定的记录得到更新。 以下是代码。
<?php
$checkbox = $_POST['pr'];
$year = $_POST['promoteyearselect1'];
$semester = $_POST['promotesemselect1'];
@ $db = mysql_connect("abc", "abc", "");
mysql_select_db("abc");
foreach($checkbox as $value){
if(isset($checkbox)){
if(($semester%2)==0) {
$strSQL = "UPDATE student SET year='".++$year."', semester='".++$semester."' WHERE enrollment='".$value."'";
$rs = mysql_query($strSQL);
if($rs){
echo 'Promotion Successful';
header("location:page1.php");
echo '<script> alert("Promotion Successful");</script>';
}
else echo "Sorry, but that did not work. ";
}
else{
$strSQL = "UPDATE student SET semester=".++$semester."' WHERE enrollment='".$value."'";
$rs = mysql_query($strSQL);
if($rs){
echo 'Promotion Successful';
header("location:page1.php");
echo '<script> alert("Promotion Successful");</script>';
}
else echo "Sorry, but that did not work. ";
}
}
}
mysql_close($db);
?>
即使我使用foreach循环来完成复选框数组,仍然只会更新第一个已检查的记录。
这是html部分
<div class="dropdown dropdown-dark">
<select name="promoteyearselect1" id="promoteyearselect1" class="dropdown-select" onfocus="showhidephdmenu()" form="promotionform" required>
<option value="">Select an option</option>
<div id="yearselect1">
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
<option value="4">4th</option>
<option value="5">5th</option>
</div>
</option>
</select>
</div>
<div class="dropdown dropdown-dark">
<select name="promotesemselect1" id="promotesemselect1" class="dropdown-select" form="promotionform" required>
<option value="">Select an option</option>
<option value="1">1st</option>
<option value="2">2nd</option>
<option value="3">3rd</option>
<option value="4">4th</option>
<option value="5">5th</option>
<option value="6">6th</option>
<option value="7">7th</option>
<option value="8">8th</option>
<option value="9">9th</option>
<option value="10">10th</option>
</select>
</div>
<button id="promotego" class="login-button" style=" position:relative; padding: 0 0 0; " onclick="getpromotestudents()"></button>
</div>
<form id="promotionform" action="promotestudents.php" method="POST">
<div id="promoteresults">
The results will show up here..!!
</div>
<div style=" position:relative; margin-top:10px; padding-left:44%;">
<input type="submit" value="Promoted" class="button black"></input>
<a href="#" class="button black">Passed Out</a>
</div>
</form>
这是获取记录并生成复选框的PHP。
$i=1;
while($r = mysql_fetch_array($rs)){
echo "<tr>";
echo "<td class='promotetabledata'>".$r[7]."</td>";
echo "<td class='promotetabledata'>".$r[6]."</td>";
echo "<td class='promotetabledata'><input type='checkbox' name='pr[]' value='".$r[7]."'/></td>";
echo "</tr>";
$i++;
}
答案 0 :(得分:1)
以下是最终适合我的修改后的代码,我在if($semester % 2 != 0)
<?php
$checkbox = $_POST['pr'];
$year = $_POST['promoteyearselect1'] + 1 ;
$semester = $_POST['promotesemselect1'] + 1;
$con = mysqli_connect("localhost","root","","university") or die("Error " . mysqli_error($con));
foreach($checkbox as $value){
if(isset($checkbox)){
// echo $value;
if( $semester % 2 != 0) {
// echo $value;
$strSQL = "UPDATE student SET year='".$year."', semester='".$semester."' WHERE enrollment='".$value."'";
$rs = mysqli_query($con, $strSQL);
if($rs){
echo 'Promotion Successful';
header("location:page1.php");
echo '<script> alert("Promotion Successful");</script>';
}
else echo "Sorry, but that did not work. ";
}
else{
$strSQL = "UPDATE student SET semester='".$semester."' WHERE enrollment='".$value."'";
$rs = mysqli_query($con, $strSQL);
if($rs){
echo 'Promotion Successful';
header("location:page1.php");
echo '<script> alert("Promotion Successful");</script>';
}
else echo "Sorry, but that did not work. ";
}
}
}
mysqli_close($con);
?>
答案 1 :(得分:0)
我没有在你的代码中找到任何名为'pr'的复选框..如果有任何..请使它成为一个数组并尝试
比如<input type="checkbox" name="pr[]">