Forum Moderators: coopster

Message Too Old, No Replies

Will not update database!?

New to php- could use some pointers!

         

finnyfin

6:27 am on Sep 10, 2006 (gmt 0)

10+ Year Member



Hello everyone

I am trying to create a script to calculate and update user's points based on the total number of players.

Here is my code so far:


<?php
ob_start();
session_start();
include("config.php");
error_reporting(E_ALL);
if (!isset($_POST[update])) {
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
echo ("<form method=\"POST\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
while ($points = mysql_fetch_array($result)) {
echo ("
<input type=\"hidden\" value=\"$points[points]\" name=\"points\">
$points[points]&nbsp;&nbsp;
<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 {
mysql_query("SELECT * FROM users");
echo ("Updated!");
$numplayers = "$_POST[numplayers]";
$played = "$_POST[played]";
$top10 = "$_POST[top10]";

$username = "$_POST[username]";
$currentpoints = "$_POST[points]";

$totalamount = 0;
$totalamount = $numplayers * 100;

$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;
$eighth = $totalamount * 0.04;
$ninth = $totalamount * 0.03;
$tenth = $totalamount * 0.02;

$playedpoints = 0;

if ($played == true) {
$playedpoints = 100;
} else {
$playedpoints = NULL;
}

$newpoints = 0;

if ($top10 == 0) {
$totalpoints = 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 == 8) {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
} else {
$newpoints = NULL;
}
$newtotal = (('$currentpoints' + '$newpoints') + 'playedpoints');
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
mysql_query($update);

}

?>

As you can see, I am trying to have the first input box calculate the total amount of points by multiplying the total amount of players by 100.

I then want a checkbox with a value of 100 points to be checked or left blank that will determine whether or not a player was in the tournament. Then, I have a dropdown with the placement in the final table from 1-10.

If the box is checked, I want only 100 points added to the user's previous points and if the box and a top10 place is present, then I want the points from the player's place and the 100 points to be added...

the problem is, right now nothing is updating.

I cannot seem to figure it out. I am also new to php programming so if anyone has any ideas on ways to make this function better, please let me know!

Thanks in advance!

dreamcatcher

7:32 am on Sep 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi finnyfin, welcome to Webmaster World. :)

First thing you need to look at is a little debugging. As nothing is updating you need to make sure the problem isn`t with the database. So, change this:

mysql_query($update);

to this:

mysql_query($update) or die(mysql_error());

Next thing to do is set your error reporting level to E_ALL, so at the top of your script add this:

error_reporting(E_ALL);

See if that throws any errors.

dc

finnyfin

7:41 am on Sep 10, 2006 (gmt 0)

10+ Year Member



Hey dc,

Thanks for the prompt response.

I am not getting any errors- none whatsoever

Here is my updated code:


<?php
ob_start();
session_start();
include("config.php");
error_reporting(E_ALL);
if (!isset($_POST['update'])) {
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
echo ("<form method=\"POST\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
while ($points = mysql_fetch_array($result)) {
echo ("
<input type=\"hidden\" value=\"$points[points]\" name=\"points\">
$points[points]&nbsp;&nbsp;
<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 {
echo ("Updated!");
$numplayers = $_POST['numplayers'];
$played = $_POST['played'];
$top10 = $_POST['top10'];

$username = $_POST['username'];
$currentpoints = $_POST['points'];

$totalamount = 0;
$totalamount = $numplayers * 100;

$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;

if ($played == true) {
$playedpoints = 100;
} else {
$playedpoints = 0;
}

$newpoints = 0;

if ($top10 == 0) {
$newpoints = 0;
} 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 == 8) {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
}
$newtotal = ($currentpoints + $newpoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
mysql_query($update) or die(mysql_error());
}

?>

The problem I am having now is simply that it is not updating EVERY user, and rather only the LAST user. So if my query returns 15 users ordered by id (1-15), only the user with the id of 15 gets updated.

finnyfin

7:52 am on Sep 10, 2006 (gmt 0)

10+ Year Member



I am getting this error though:

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #3' at line 1 Whole query: Resource id #3

finnyfin

10:16 am on Sep 10, 2006 (gmt 0)

10+ Year Member



any ideas?

someone told me that i should use arrays as my input names:

EXAMPLE:

input type=\"hidden\" value=\"$points[points]\" name=\"points[]\">
$points[points]&nbsp;&nbsp;
<input type=\"checkbox\" value=\"played\" name=\"played[]\">
<select name=\"top10[]\" value=\"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[]\">

but i do not know how to do this correctly...

any help?

is this the right direction?

jatar_k

5:15 pm on Sep 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I looked through the code and that particular error usually means you aren't processing a value from a db correctly

I didn't really think it came from that actual update so I thought it might come from the previous generation

look at the source code for the page you are using to update

it might actually have 'Resource id #3 ' as one of your values

I also noticed some things in your code which may cause issues so I reformatted it to this

<?php
ob_start();
session_start();
include("config.php");
error_reporting(E_ALL);
if (!isset($_POST['update'])) {
$result = mysql_query("SELECT * FROM users ORDER BY points DESC");
echo ("<form method=\"POST\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");
if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
while ($points = mysql_fetch_array($result)) {
echo "<input type=\"hidden\" value=\"",$points['points'],"\" name=\"points\">",$points[points],"&nbsp;&nbsp;"
?>
<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>
<?php
echo "<input type=\"hidden\" value=\"",$points[username],"\" name=\"username\">";
echo "<a href=\"members.php?user=",$points[username],"\">",$points[username],"</a><br />\n";
}
echo "<br><br><input type=\"submit\" value=\"Calculate!\" name=\"update\"></form>";
}
} else {
echo ("Updated!");
$numplayers = $_POST['numplayers'];
$played = $_POST['played'];
$top10 = $_POST['top10'];

$username = $_POST['username'];
$currentpoints = $_POST['points'];

$totalamount = 0;
$totalamount = $numplayers * 100;

$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;

if ($played == true) {
$playedpoints = 100;
} else {
$playedpoints = 0;
}

$newpoints = 0;

if ($top10 == 0) {
$newpoints = 0;
} 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 == 8) {
$newpoints = $eigth;
} elseif ($top10 == 9) {
$newpoints = $ninth;
} elseif ($top10 == 10) {
$newpoints = $tenth;
}
$newtotal = ($currentpoints + $newpoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
mysql_query($update) or die(mysql_error());
}

?>

this escapes from php parsing to output that chunk of html and uses better echo syntax for your arrays, which may not have been resolving inside double quotes.

give those two things a try and tell us what happens

you should also echo your constructed query before sending it to mysql like so

$newtotal = ($currentpoints + $newpoints) + $playedpoints;
$update = "UPDATE users SET points = '$newtotal' WHERE username = '$username'";
echo '<p>update query:<br>',$update;
mysql_query($update) or die(mysql_error());

then you can actually look at the query and see if it looks right. You could also paste it into the command line or into phpmyadmin to test it

finnyfin

4:50 am on Sep 12, 2006 (gmt 0)

10+ Year Member



Hello and thanks for the post-

I have several different versions of this code now and none of them will work. I have created loops and used foreach statements and for statements and I simply cannot get this script to update each user individually. I have gotten it to update one user on the list, and update each user with one set of points, but not each user individually.

It is pretty frustrating.

By the way, the formatting you did only had simple errors... $variable[value] instead of $variable['value']

jatar_k

6:12 am on Sep 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



did you try echo'ing your query as I mentioned?

did you check your source to see if the error was being generated previously?

>> the formatting you did only had simple errors

did you notice that some of those were concatenation errors that wouldn't actually resolve in the way they were?

such as the arrays within echo's which will only resolve properly when they are surrounded by braces

echo your query and see what it looks like, don't worry about sending them to the db, just print all of them to the screen on individual lines and see if that shows where the error is.

I could probably write your update script in a minute or two but that wouldn't really help anyone learn anything. Go with what you had above and try to see if you can isolate why it is misbehaving. Have it spit out some variables to the browser and diagnose it, don't try and rewrite it every time. Whenever I do that I usually make things worse unless I go back and start from scratch.

finnyfin

7:02 am on Sep 12, 2006 (gmt 0)

10+ Year Member



thank you for the advice and motivation- i really appreciate it!

the echo returned the following:

UPDATE users SET points = '2800' WHERE username = 'test1'UPDATE users SET points = '2800' WHERE username = 'test1'

test1 was the last user on the list. 2800 is an accurate calculation of the total points. it is odd that it only repeated it twice- i would have expected it to have repeated 4 times (since there are 4 users in the database)

finnyfin

7:18 am on Sep 12, 2006 (gmt 0)

10+ Year Member



i am assuming i have to setup a for() or foreach() statement for my update... but i have tried it but i cant seem to get it right...

i am also having an issue with the checkbox... as of now, if a single box is checked, it effects every user. i obviously want it to be individual.

jatar_k

6:02 pm on Sep 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



can you post your create table for the users table and then maybe I can play around and see what is happening

finnyfin

2:15 am on Sep 13, 2006 (gmt 0)

10+ Year Member



CREATE TABLE `users` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(30) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`email` varchar(40) NOT NULL default '',
`yahoo` varchar(250) NOT NULL default '',
`msn` varchar(250) NOT NULL default '',
`aim` varchar(250) NOT NULL default '',
`favhand` varchar(250) NOT NULL default '',
`occupation` varchar(250) NOT NULL default '',
`location` varchar(36) NOT NULL default '',
`online` varchar(12) default NULL,
`level` int(1) default '1',
`photo` varchar(255) NOT NULL default 'default.gif',
`about` varchar(255) NOT NULL default '',
`points` varchar(5) NOT NULL default '00000',
`name` varchar(50) NOT NULL default '',
`rank` int(5) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

finnyfin

5:51 am on Sep 13, 2006 (gmt 0)

10+ Year Member



im still having the same issues, although my query update is looking better.

my checkbox still effects every user, even if only one is checked, and my top10 system still isnt working

finnyfin

5:53 am on Sep 13, 2006 (gmt 0)

10+ Year Member



sorry i posed the wrong code earlier:

<?php

session_start();

include("config.php");

error_reporting(E_ALL);

if (!isset($_POST['update'])) {
$result = mysql_query("SELECT * FROM users ORDER BY points DESC") or die(mysql_error());

echo ("<form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">");
echo ("<input type=\"text\" name=\"numplayers\" size=\"3\" maxlength=\"3\"><br><br>");

if (mysql_num_rows($result) == 0) {
echo "<strong><p>There are no players!</p></strong>";
} else {
while ($points = mysql_fetch_array($result)) {
echo ("
<input type=\"hidden\" value=\"$points[points]\" name=\"points[]\">
$points[points]&nbsp;&nbsp;
<input type=\"checkbox\" value=\"1\" 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 {

echo ("Updated!<br>");

$numPlayers = $_POST['numplayers'];
$Played = $_POST['played'];
$top10 = $_POST['top10'];

$Username = $_POST['username'];
$Points = $_POST['points'];

$totalamount = $numPlayers * 100;

if ($Played == true) {
$playedpoints = 100;
} else {
$playedpoints = 0;
}

if ($top10 == 1) {
$newpoints = $totalamount * 0.30;
} elseif ($top10 == 2) {
$newpoints = $totalamount * 0.20;
} elseif ($top10 == 3) {
$newpoints = $totalamount * 0.12;
} elseif ($top10 == 4) {
$newpoints = $totalamount * 0.10;
} elseif ($top10 == 5) {
$newpoints = $totalamount * 0.08;
} elseif ($top10 == 6) {
$newpoints = $totalamount * 0.06;
} elseif ($top10 == 7) {
$newpoints = $totalamount * 0.05;
} elseif ($top10 == 8) {
$newpoints = $totalamount * 0.04;
} elseif ($top10 == 9) {
$newpoints = $totalamount * 0.03;
} elseif ($top10 == 10) {
$newpoints = $totalamount * 0.02;
} else {
$newpoints = 0;
}

foreach($Username as $i=>$User)
{
$npoints = $newpoints;
$cpoints = $Points[$i];

$newtotal = ($cpoints + $npoints) + $playedpoints;

$update = "UPDATE users SET points = '$newtotal' WHERE username = '$User'";

mysql_query($update) or die(mysql_error());

echo "<br><br>$update<br>";

}

}

?>

finnyfin

4:33 am on Sep 14, 2006 (gmt 0)

10+ Year Member



i figured it out- thanks anyway though guys