duan198727 2014-10-20 12:34
浏览 29

基于复选框更新表,

Hello Hope every one will be good. I have a problem.

I have a form following form code.

<form name="form3" method="post" action="">

    <?php while ($row_news1 = mysql_fetch_assoc($rs_news1)) { ?>

<input type="checkbox" name="check1[]" value="1" <?php if($row_news1['house']=='1') echo "checked=\"checked\""; ?>>


<input type="checkbox" name="check2[]" value="1" <?php if($row_news1['add_item']=='1') echo "checked=\"checked\""; ?>>



<input type="text" name="user_name[]" id="textfield" readonly style="width:50%;color:white;background-color:transparent" value="<?php echo $row_news1['FirstName'].'&nbsp;'.$row_news1['LastName'];  ?>"><br>


<input type="hidden" name="user_id[]" value="<?php echo $row_news1['UserID']; ?>">


<?php } ?>


<input type="submit" value="Submit" name="update" class="but">     

<input type="submit" value="Reset" name="reset" class="but">

</form>

I want that how much the user name record comes to an array. That much time the two check boxes will be Generated. So when i checked all the check boxes so the table update successfully. But when i want to checked some check boxes randomly.so their corresponding record will be update with Value 1 and all other unchecked check boxes Corresponding Record will be updated with the Value of 0.

Here is my Form Submission code.

if(isset($_POST['update'])&& $_POST['user_name']!=''){  // checking if Update button clicked

 $user_id=$_POST['user_id']; // Getting all user_id

 $user_name= $_POST['user_name']; //Getting user name text box

 $check1=$_POST['check1'];// Getting all check boxes but in this case its only taking checked boxes.

 $check2=$_POST['check2'];


        for($i = 0; $i < count($user_name); $i++){ // iterating the code until we have user name.

            if($check1[$i]=='1'){ // checked if first checkbox value is 1 so assign 1 to Variable a. other wise 0;

                $a=1;

            }

            else {

                $a=0;


            }

            if($check2[$i]=='1'){ // same as above i want

                $b=1;

            }

            else {

                $b=0;

            }

           // this is the query that i want to update the rocord

            $query_u1 = "UPDATE Users

                SET

                house = '".$a."',

                add_item = '".$b."'

                WHERE UserID=".$user_id[$i];

        mysql_query($query_u1) or die(mysql_error());


        }

        echo '<script> window.location = "adminmain.php";</script>';

}

The above code shows my Php and mysql query code.

Now i Want that i get all check boxes checked or not. just the other things. Means i have 10 Record for users. Every record have their UserId and name. With Every Record I have Two check Boxes as show in code.

I want when i checked 5 check boxes Randomly. so these five corresponding record will be updated with 1 value. and other five check boxes corresponding record will updated with 0 value.

I think you people will got it now.

  • 写回答

1条回答 默认 最新

  • dpzr52746 2014-10-20 15:49
    关注

    Ok, this is actually very simple to do. Start with a basic form:

    <form method="post" action="">
        <input type="checkbox" name="my_checkboxes[]" checked>
        <input type="checkbox" name="my_checkboxes[]">
        <input type="checkbox" name="my_checkboxes[]">
    </form>
    

    Then build in your back-end with php

    if(isset($_POST['my_checkboxes'])){
       foreach($_POST['my_checkboxes'] as $key => $val){
            // Update database with $val Update DB set col = '$val' (use prepared statements for security)
       }
    }
    

    This code can be much improved, but this will give you the general idea. One important note, php will only pass the checked checkboxes. So, keep that in mind.

    One way to overcome this potential issue is to add a hidden input for each checkbox that you have, so your forum might change to look like this (note: removing brackets from the name attribute):

    <form method="post" action="">
        <input type="checkbox" name="my_checkbox1" value="sent" checked>
        <input type="hidden" name="my_checkbox1" value="">
        <input type="checkbox" name="my_checkboxe2">
        <input type="hidden" name="my_checkbox2" value="">
    </form>
    

    Now, using PHP you would just have to determine if the value is sent or if the value is empty. If the value is sent, it's checked, otherwise it's unchecked.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看