使用php更新带有多个复选框的html表单

时间:2014-12-01 14:09:15

标签: php checkbox multiple-columns updating

我有一个html表单,我想检查表单中的一些项目,然后更新表单。 (实际上没有顺序选中复选框,它是完全随机的) 我的问题是,当我标记一些复选框并按更新时,我的代码将按顺序标记顶部复选框。 这就是我的意思:

提交前

enter image description here

提交后:enter image description here

这是我的表单代码:

$row_counter=0;                     

if ($_SESSION['user_row_num']=="1")
{
?>
<form method="POST" action="" name="frm1">
    <table class='styled-table' cellspacing='0' border='1'>
        <tr>
            <th  scope='col' style='font-size:13px;'>number</th>
            <th  scope='col' style='font-size:13px;'>view</th>
            <th  scope='col' style='font-size:13px;'>edit</th>
            <th  scope='col' style='font-size:13px;'></th>      
        </tr>               
<?php   while($row_form = mysqli_fetch_assoc($query_formsearch))
        { 
            //fetching from profile previous valuse of form access
            $query_profile_check = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION['user_id']}' ");
            $row_profile = mysqli_fetch_assoc($query_profile_check);

            //creating form name from forms table for profile table
            $profile_form_name="form_".$row_form['num'];
            $profile_frm_name=$row_profile[$profile_form_name];

            $a_form_aces=explode("-", $profile_frm_name);
            $frm_view=$a_form_aces[0];
            $frm_edit=$a_form_aces[1];?>    

        <tr>
            <td align='center'><input  class='styled-input' type='hidden' name='form_num[]' id='form_num' value="<?php echo $row_form['num']; ?>"/></td>
            <td align='center'><input  class='styled-input' type='text' name='form_name[]' id='form_name' value="<?php echo $row_form['name']; ?>"/></td>
            <td align='center'><input  class='styled-input' type='checkbox' name='view[]' id='view' <?php if($frm_view=="1")echo "checked"; ?> /></td>
            <td align='center'><input  class='styled-input' type='checkbox' name='edit[]' id='edit' <?php if($frm_edit=="1")echo "checked"; ?> /></td>
            <td align='center'><input  type='hidden' name='row_counter' id='row_counter' value="<?php echo $row_counter++; ?>"/></td>
        </tr>   
<?php   } ?>
    </table>

    <input  class='styled-input_2' style='padding: 5px; width: 140px;' type='submit' name='save_setting' id='save_setting' value="update" >
    <div class='cleaner h30'></div> 
    </form>

这是我的更新代码:

<?php   
    } // end of if($_SESSION['user_row_num']=="1")                  



// Check if button name "edit-msb" is active, do this 
    if(isset($_POST['save_setting']) && $_POST['save_setting'] == 'update')
    {

            for($i=0;$i<=$_SESSION['user_count'];$i++)
            {
                $row_no = ($_REQUEST['row_counter'][$i]);
                $form_numb = $_REQUEST['form_num'][$row_no];

                if(isset($_REQUEST['view'][$row_no])){$_REQUEST['view'][$row_no]="1";}else{$_REQUEST['view'][$row_no]="0";}
                if(isset($_REQUEST['edit'][$row_no])){$_REQUEST['edit'][$row_no]="1";}else{$_REQUEST['edit'][$row_no]="0";}

                $form_access=$_REQUEST['view'][$row_no]. "-" .$_REQUEST['edit'][$row_no];
                $profile_form_num="form_". $form_numb;

                $access_query=mysqli_query($con,"UPDATE `profile` SET `{$profile_form_num}`='{$form_access}' WHERE `id`='{$_SESSION['user_id']}'");
            }

            if($access_query!='')
            {
                echo "<div class='cleaner h30'></div>";
                echo "<b style='color:green;margin-left:10px;font-size:15px;'>the form successfully updated.</b>";

            }




    }                   
?>      

我希望每个复选框在我提交表单后按照我标记的顺序显示它的更新值,但我不知道我的错误在哪里。

1 个答案:

答案 0 :(得分:0)

我已将您的代码从复选框更改为选择选项并且正在查看它是否适合您

表单代码:

<form method="POST" action="" name="frm1">
 <table class='styled-table' cellspacing='0' border='1'>
    <tr>
    <th  scope='col' style='font-size:13px;'>form number</th>
    <th  scope='col' style='font-size:13px;'>form name</th>
    <th  scope='col' style='font-size:13px;'>view</th>
    <th  scope='col' style='font-size:13px;'>edit</th>
    <th  scope='col' style='font-size:13px;'></th>      
    </tr>               
<?php   while($row_form = mysqli_fetch_assoc($query_formsearch))
        { 
            //fetching from profile previous valuse of form access
            $query_profile_check = mysqli_query($con,"SELECT * FROM `profile` WHERE `id`='{$_SESSION['user_id']}' ");
            $row_profile = mysqli_fetch_assoc($query_profile_check);

            //creating form name from forms table for profile table
            $profile_form_name="form_".$row_form['num'];
            $profile_frm_name=$row_profile[$profile_form_name];

            $a_form_aces=explode("-", $profile_frm_name);
            $frm_view=$a_form_aces[0];
            $frm_edit=$a_form_aces[1];?>    

    <tr>
        <td align='center'><input  class='styled-input'  type='text' name='form_num[]' id='form_num' value="<?php echo $row_form['num']; ?>"/></td>
        <td align='center'><input  class='styled-input'  type='text' name='form_name[]' id='form_name' value="<?php echo $row_form['name']; ?>"/></td>
        <td align='center'>
            <select class='styled-input'  name='view[]' id='view'  />
                <option value="<?php echo $frm_view; ?>"><?php if($frm_view=="1"){echo "yes";}else{echo "no";} ?></option>
                <option></option>
                <option value="1" style="color:#1A75FF;">yes</option>
                <option value="0" style="color:red;">no</option>
            </select>
        </td>
        <td align='center'>
            <select class='styled-input' name='edit[]' id='edit' />
                <option value="<?php echo $frm_edit; ?>" ><?php if($frm_edit=="1"){echo "yes";}else{echo "no";} ?></option>
                <option></option>
                <option value="1" style="color:#1A75FF;">yes</option>
                <option value="0" style="color:red;">no</option>
            </select>
        </td>
        <td align='center'><input  type='hidden' name='row_counter' id='row_counter' value="<?php echo $row_counter++; ?>"/></td>
     </tr>   
<?php   } ?>
</table>

更新代码:

<?php   

// Check if button name "edit-msb" is active, do this 
    if(isset($_POST['save_setting']) && $_POST['save_setting'] == 'update')
    {


            for($i=0;$i<$_SESSION['user_count'];$i++)
            {
                //$row_no = ($_REQUEST['row_counter'][$i]);


                $form_access=$_POST['view'][$i]. "-" .$_POST['edit'][$i];
                echo $profile_form_num="form_". $_POST['form_num'][$i];echo"<br>";
                echo $form_access;echo"<br>";

                $access_query=mysqli_query($con,"UPDATE `profile` SET `{$profile_form_num}`='{$form_access}' WHERE `id`='{$_SESSION['user_id']}'");


            }

            if($access_query!='')
            {
                echo "<div class='cleaner h30'></div>";
                echo "<b style='color:green;margin-left:10px;font-size:15px;'>form successfully updated.</b>";
            }

    }                   
?>