0

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></center>
<?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%.

15
  • Horrible code. Format your code properly, and cut it down to what is essential to the question. Thanks two tons. Commented Aug 9, 2011 at 20:56
  • @Shef It actually is indented properly, perhaps shy the if/else. Also, I cut / pasted at some rather odd points since I didn't want to hand out my database details, can you blame me? Also: I'm not really too sure what I can exclude from the question... Commented Aug 9, 2011 at 20:58
  • Your update loop isn't the same as the one I posted. The mysql_query("UPDATE attend set rTotal=(rTotal+1) WHERE UserName='$member'"); should be outside of the foreach loop. Commented Aug 9, 2011 at 21:02
  • @Kyle- The reason that I couldn't use that code specifically is because every user needed to have their own rTotal variable to show how many total raids have occurred since they joined the guild. If I were to update that outside the foreach wouldn't I just be left with one number for everyone? Ex: If someone joined after 5 raids, their first raid would then mean they had attended 1 raid / missed 5? Am I off base with this assertion? Commented Aug 9, 2011 at 21:05
  • @Kyle: Also, I still am not able to pass whether or not the person was in the raid via the checkbox, since the checkbox already had a value field filled by the membername? Normally when I use a checkbox I have the value equal to 1, or something along those lines...in this case, I think I'm passing null? Or I'm just generally mis-handling it? Commented Aug 9, 2011 at 21:06

2 Answers 2

2

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'];
Sign up to request clarification or add additional context in comments.

1 Comment

That's a really good point too... I honestly didn't even think of that. This is going to take quite a bit of debugging!
1

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

8 Comments

No joke, just spent like 4 hours with this guy, pretty much have to give him the credit for this answer for one query alone!
Hey Kyle, can you check your mail, I screwed something up?!
can you check your mail, sorry, I didn't link that right last time.
Sorry, have been so busy. Yes. Give me about another hour so I can get to a point where I can take a look at it. In the meantime, would you please email me a little more information about what exactly it is doing wrong?
I shot you a mail about it a few days ago, it was pretty detailed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.