Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更新选定复选框值的sql表_Php_Html_Forms - Fatal编程技术网

Php 更新选定复选框值的sql表

Php 更新选定复选框值的sql表,php,html,forms,Php,Html,Forms,我使用复选框动态创建表行。检查其中一些,然后对所选的执行并更新查询。但我面临的问题是,即使我使用了foreach循环,也只有第一个选定的记录得到更新。 下面是代码 <?php $checkbox = $_POST['pr']; $year = $_POST['promoteyearselect1']; $semester = $_POST['promotesemselect1']; @ $db = mysql_connect("abc

我使用复选框动态创建表行。检查其中一些,然后对所选的执行并更新查询。但我面临的问题是,即使我使用了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);               

?>

我在您的代码中未找到任何名为“pr”的复选框。如果有。请将其设置为数组并重试

我在你的代码中没有找到任何名为“pr”的复选框。如果有。请将其设置为数组并重试
就像

这里是最终对我有效的修改后的代码,我在
if($Semment%2!=0)


这是最终对我有效的修改后的代码,我在
if($sement%2!=0)


欢迎使用堆栈溢出。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,。在php代码中,您使用的是一个名为“pr”的变量,但我在HTML代码段中没有看到带有此名称的复选框。请显示它们。@user4035在最后添加了PHP代码,生成了名为“pr”的复选框@DaveChen我切换到mysqli API,仍然只更新了第一条记录,其余记录没有更新:/@AkashGupta将var_转储($checkbox);在foreach之前给我看看结果。我在你的代码中没有看到任何重要的错误。仅当(isset($checkbox))离开forach并放在foreach之前时才移动。无需检查,数组是否一直在定义。欢迎使用堆栈溢出。它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,。在php代码中,您使用的是一个名为“pr”的变量,但我在HTML代码段中没有看到带有此名称的复选框。请显示它们。@user4035在最后添加了PHP代码,生成了名为“pr”的复选框@DaveChen我切换到mysqli API,仍然只更新了第一条记录,其余记录没有更新:/@AkashGupta将var_转储($checkbox);在foreach之前给我看看结果。我在你的代码中没有看到任何重要的错误。仅当(isset($checkbox))离开forach并放在foreach之前时才移动。无需检查,数组是否一直在定义。
<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>
$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++;
                }
   <?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);             

    ?>