开发者

Using input checkboxes with a database

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.

Thanks for any help!

Edit: 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.

Edit 2: Stray }


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.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.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)");


on form:

<input type=checkbox name=selected[] value='" . $f1 . "'>

on raidattend.php:

$selected=$_POST['selected'];

while (list ($key,$val) = @each ($selected)) {

    //$val will hold the username

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜