在数据库中使用输入复选框 [英] Using input checkboxes with a database

查看:107
本文介绍了在数据库中使用输入复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正试图通过向公会写一个出勤跟踪器来帮助朋友,以实现突袭和其他目的。现在,我的概念是从用户列中进行选择*,并为每个用户创建一个复选框,假设该人出现了突袭,它将在表单中传递 1,并且突袭出席率将​​增加1.在用户页面上,总出席人数将计算为(raidAtt / raidsTotal)* 100(自加入以来)。

So I'm trying to help a friend out by writing his guild an attendance tracker for raiding and whatnot. Right now my concept is to do a select * from the user column, and make a checkbox for each user, assuming that person showed up to raid, it would pass a "1" through the form, and their raid attendance would be incremented by 1. On the users page the overall attendance would be calculated as (raidAtt / raidsTotal)*100 (since joining).

我现在的问题是我真的不知道如何使用单个循环传递所有这些信息...

My issue right now is that I don't really know how to get all this information passed using a single loop...

现在我的代码是这样的:

Right now my code is something like this:

            <form action="raidattend.php" method="post">
            <?php 

                mysql_connect("$database",$username,$password);
                @mysql_select_db($database) or die( "Unable to select database");
                $query="SELECT * FROM attend WHERE UserName = $v_member ORDER BY date desc";
                $result=mysql_query($query);

                $num=mysql_numrows($result);

                mysql_close();
                ?>
            <table>
            <tr>
            <th>Member</th>
            <th>Attended?</th>
            </tr>   
            <?php
                $i=0;
                while ($i < $num) {
                $f1=mysql_result($result,$i,"UserName");
            }
            <tr>
            <td><?php echo $f1; ?></td><td input type="checkbox" checked value="1">

这就是我遇到的问题。我不确定如何将每个用户以及复选框的结果传递回数据库。一旦我了解了操作方法,就跟增加操作一样简单,但是我却迷失了。

And that's where I ran into issues. I'm not sure how to pass each user and the result of the checkbox back to the database. Once I understand how to do that it's just as simple as incrementing, but I'm pretty lost.

感谢您的帮助!

编辑:为明确起见,我不确定如何拆分它,以便每个成员都得到更新,我了解我需要使用提交和所有这些内容。

To clarify, what I'm unsure of is how to break it up so each member gets updated, I understand that I need to use a submit and all that.

编辑2:Stray}

Edit 2: Stray }

推荐答案

您应该更改复选框,以使它们都具有相同的名称(即name = member [])。这样,当您提交表单时,所有选中的成员都将位于$ _POST [’member’]中。然后,只需遍历$ _POST ['member']并更新您的表即可。

You should change your checkbox so that they all have the same name (ie name="member[]"). This way, when you submit your form, all of the checked members will be in $_POST['member']. Then, just loop through $_POST['member'] and update your table.

<td><?php echo $f1; ?><td> <input type="checkbox" name="member[]" value=<?php echo "'$f1'"; ?> /></td>

这应该为您提供复选框列表,其中包含与会成员的姓名。

This should give you the list of checkboxes with the names of the members that attended.

这里是如何进行更新的快速概述:

Here is a quick overview of how to do the update:

1。通过$ _POST ['member']和增加该人参加的金额:

1.Loop through $_POST['member'] and increment the amount that person has attended :

 foreach($_POST['member'] as $member)
 {
    mysql_query("update table_name set attended=(attended+1) where username='$member'");
 }

2。更新每个参加的成员后,请对整个成员进行更新表格以增加已发生的突袭总数:

2.After you update each member that attended, do an update on the entire table to increment the total number of raids that have happened:

mssql_query("update table_name set total=(total+1)");

这篇关于在数据库中使用输入复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆