开发者

Using input checkboxes with a database--part 2

开发者 https://www.devze.com 2023-03-27 01:09 出处:网络
Warning:This question might be a bit long, my apologies in advance. So in my last question seen here: Using input checkboxes with a database

Warning: This question might be a bit long, my apologies in advance.

So in my last question seen here: Using input checkboxes with a database I asked the question: "How do I manage multiple users raid attendance with checkboxes and a database loop" and I got a solution that worked in the shortrun, but failed in the longer-run.

Here's the code that runs the loop / allows the user to select who raided: checked />

When I add this to the database, I actually use 3 queries, shown here:

foreach($_POST['member'] as $member)
{
    mysql_query("UPDATE attend set rAttend=(rAttend+1) WHERE UserName='$member'");
    mysql_query("INSERT INTO attend set rDate =(CURDATE()) WHERE UserName='$member'");
    mysql_query("UPDATE attend set rTotal=(rTotal+1) WHERE UserName='$member'");
}

The reason why I can't use a single 'total' is because each user needs to have the total be based off the amount of raids they attended. Right now the page is displaying like this:

http://i.imgur.com/dwxLf.png

Despite the fact that I entered a date (with CURDATE()) and had selected the checkbox to be checked.

Here's the full code for the query that displays the above: (warning long)

    $query="SELECT rTotal FROM rAttend WHERE Username=('$v_member')";
    $total=mysql_query($query) or die(mysql_error());

    $query="SELECT * FROM rAttend WHERE UserName =('$v_member') order by UserName";
    $result=mysql_query($query);
    $num=mysql_num_rows($result);
?>
<center><h3><?php echo ($v_member)?>'s attendence record</h3></c开发者_StackOverflowenter>
<?php
$i=0;
$j=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"rDate");
$f2=mysql_result($result,$i,"UserName");
$f3=mysql_result($result,$i,"rAttend");
?>

<tr>
<td><?php echo $f1; ?></td>
<td><?php echo '<a href="'.$f2.'.php">'.$f2.'</a>'; ?></td>
<?php if ($f3 == 1){
echo "<td>yes"; $j++;
}else{ echo "<td>no" ;} ?></td>
</tr>

<?php
$i++;
}
?>
<center>"Raid Attendence: "<?php echo ($j/$total)*100; ?> %</center><br />
</table>

If anyone could help me debug this, I would be most grateful, as php / mysql has never been my favorite language.

Thanks a TON!!!

Edit 1: Shortened posted code by about 30%.


On the second line of your display code:

$total=mysql_query($query) or die(mysql_error());

It seems you're expecting $total to be a number, but it's actually a resource (that's what mysql_query does, it returns a resource to the resultset). You need something like this:

$total_query = mysql_query($query) or die(mysql_error());
$total_row = mysql_fetch_array($total_query);
$total = $total_row['rTotal'];


After working for this on the past few hours, HunderThooves and I finally got the solution. Haha.

0

精彩评论

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