however, it is only updating the last user on the list, not all of them. also, how would i go about adding the points to the database and not overwriting them each time i hit the submit button?
Your code will only update the record for $username. But even that is not going to work unless you created an array called $get and used that in your form.
you store number of players in $numplayers variable... but are you returning all the usernames in the array.. I suppose only last ne gets passed and is updated in the end.
ah, I see it.. you are outputting multiple players along wth their attributes for updattion? in this case you should use arrays as field names... like f.e. take username fld...
i unfortunately cannot get it to work. i keep running into the same issues- i can get it to loop, i just cant get it to loop one instance, so if i enter a value for one user, it applys the value to every user. i am going to settle with a setup that is a little more simple but less appealing... pretty dissapointing...
thanks anyway though. if anyone has any ideas or pointers, please feel free to post them. i cannot let this beat me so i will still try to make it happen, but for now i need to get this site up.
<?php
// Why are you using output buffering?
ob_start();
session_start();
include("config.php");
error_reporting(E_ALL);
/*
This is checking to see if the POST array var 'update'
is NOT set. I suppose this assumes a posted form?
*/
if (!isset($_POST['update'])) {
// There is no error checking on this query
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
// There is no action here
echo ("<form method=\"POST\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
// Check to see if there was no returned result
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
/*
This is now looping to display records from the query
This will create 2 hidden fields, a checkbox and a
select element for each record in the `users` table
*/
while ($points = mysql_fetch_array($result)) {
echo ("
<input type=\"hidden\" value=\"$points[points]\" name=\"points\">
$points[points]
<input type=\"checkbox\" value=\"played\" name=\"played\">
<select name=\"top10\">
<option value=\"0\">0
<option value=\"1\">1st
<option value=\"2\">2nd
<option value=\"3\">3rd
<option value=\"4\">4th
<option value=\"5\">5th
<option value=\"6\">6th
<option value=\"7\">7th
<option value=\"8\">8th
<option value=\"9\">9th
<option value=\"10\">10th
</select>
<input type=\"hidden\" value=\"$points[username]\" name=\"username\">
<a href=\"members.php?user=$points[username]\">$points[username]</a><br />\n");
}
echo ("<br><br><input type=\"submit\" value=\"Calculate!\" name=\"update\"></form>");
}
} else {
/*
If we are here then the form field called 'update' was
posted. But you are not checking for values in any other
form field that was posted.
*/
echo ("Updated!");
/*
Your syntax for each of these is wrong
You may want to change them to:
$numplayers = $_POST['numplayers'];
and so on throughout the script
*/
$numplayers = "$_POST[numplayers]";
$played = "$_POST[played]";
$top10 = "$_POST[top10]";
$username = "$_POST[username]";
$currentpoints = "$_POST[points]";
$totalamount = 0;
// You realize that if $totalamount is 0 then this will be 0 also?
$totalamount = $numplayers * 100;
// And if total amount was 0 above, then it will 0 in all of these?
$first = $totalamount * 0.30;
$second = $totalamount * 0.20;
$third = $totalamount * 0.12;
$fourth = $totalamount * 0.10;
$fifth = $totalamount * 0.08;
$sixth = $totalamount * 0.06;
$seventh = $totalamount * 0.05;
$eigth = $totalamount * 0.04;
$ninth = $totalamount * 0.03;
$tenth = $totalamount * 0.02;
$playedpoints = 0;
// This may not evaluate the way you think based on your assingment syntax above
if ($played == true) {
$playedpoints = 100;
} else {
// You already set this a few lines up, you don't need this 'else'
$playedpoints = 0;
}
$newpoints = 0;
/*
With the exception of the first conditional in this list
every other conditional will result in 0 based on the 0
multiplier above.
You may also want to consider a switch statement here
*/
if ($top10 == 0) {
$newpoints = NULL;
} elseif ($top10 == 1) {
$newpoints = $first;
} elseif ($top10 == 2) {
$newpoints = $second;
} elseif ($top10 == 3) {
$newpoints = $third;
} elseif ($top10 == 4) {
$newpoints = $fourth;
} elseif ($top10 == 5) {
$newpoints = $fifth;
} elseif ($top10 == 6) {
$newpoints = $sixth;
} elseif ($top10 == 7) {
$newpoints = $seventh;
} elseif ($top10 == {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
} else {
$newpoints = 0;
}
// This will more than likely be $currentpoints + 100 (or + 0)
$newtotal = ($currentpoints + $newpoints) + $playedpoints;
// this will update the one user for whom the form was posted
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
/*
Again, you are not using any error checking.
How are you going to know if something went wrong?
*/
mysql_query($update);
}
?>
As of now my querys are much better than before. I still cannot get the checkbox to effect each user individually. If one is checked, it will effect every user. I also still cant get the top10 to work...