...

View Full Version : mysql pho Leaderboard help



elcubanoluis
09-14-2011, 08:31 PM
I am trying to write a script in which users can go to my site and update the leaderboard.

Need to have the option of adding new people and updating people.

also need the database to display in a column on the site. and ordered in a decending order by players points.

i am having a lot of trouble with the script.

Can anyone help?

BluePanther
09-14-2011, 08:57 PM
Post your script and let us know the specific problems you're having :)

elcubanoluis
09-15-2011, 07:50 AM
this is the code for the form i am using.


<form action="leaderboard.php" mehtod="POST" />
<p><br />
<br />
<br />
</p>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td><div align="right">Points: </div></td>
<td><input type="text" name="points"/></td>
</tr>
<tr>
<td><div align="right">First Name: </div></td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
<p></p>
<p>&nbsp;</p>


this is the code for the script.


<?PHP

define('db_name', 'webadmin_leaderboard');
define('db_user', 'webadmin');
define('db_password', 'collide198');
define('db_host', "localhost");

$link = mysql_connect(db_host, db_user, db_password);

if (!$link) {
die('Could Not Connect: ' . mysql_error());
}

$db_selected = mysql_select_db(db_name, $link);

if (!$db_selected) {
die('can\'t use' . db_name . ': ' . mysql_error());
}

echo 'Connected Successfully';



$Points = $_POST[`points`];

$sql = "INSERT INTO leaderboard (`points`) VALUES ('$points')";

$name = $_POST[`name`];

$sql = "INSERT INTO leaderboard (`name`) VALUES ('$name')";



if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}

mysql_close();

?>

right now the problem i am having is when i use the form to insert information, it does not insert the actual information, it starts a new line but only puts '0' in the fields.

also, i am not sure how to write the code to display the database on the site in decending order by the "points" column.

I also need to write a code for a form or edit the original form to "update" an existing players points without getting the "duplicate entry" error message.

in the database i have 3 columns.

ID - INT - AI - Index
points - int - primary
name - varchar - unique

Wanna
09-15-2011, 08:42 AM
The scripts puts a 0 because PHP is case sensative


$points = 3;
echo $Points;
// Returns: FALSE (False is also a '0')


Change this piece of code:


$Points = $_POST[`points`];

$sql = "INSERT INTO leaderboard (`points`) VALUES ('$points')";


to:


$points = $_POST[`points`];

$sql = "INSERT INTO leaderboard (`points`) VALUES ('".$points."')";


and it should work

and i'm guessing this should't work, because you overwrite the variable:


$Points = $_POST[`points`];

$sql = "INSERT INTO leaderboard (`points`) VALUES ('$points')";

$name = $_POST[`name`];

$sql = "INSERT INTO leaderboard (`name`) VALUES ('$name')";


Change that to:


$points = $_POST[`points`];

$name = $_POST[`name`];

$sql = "INSERT INTO leaderboard (`name`,`points`) VALUES ('".$name."','".$points."')";


And you database table isn't right either.
Because a primary key can only exists once in the table.
You set the column 'points' as primary key, but there can be a name with the same amount of points and in the current setting this won't work.

To get things out of database use this:


$sql = "SELECT * FROM leaderboard ORDER BY `points` DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_object($result))
{
// Do somestuff with this:
// To get something: $row->column_name
// They code inside here will be excecuted as many times as there are rows in the database.
}

elcubanoluis
09-16-2011, 08:13 AM
I changed the code like you suggested,

using the following:

Form:


<form action="leaderboard.php" mehtod="POST" />
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td><div align="right">Points: </div></td>
<td><input type="text" name="Points"/></td>
</tr>
<tr>
<td><div align="right">Name: </div></td>
<td><input type="text" name="Name"/></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
<p><br />
<br />
<br />
</p>
<p></p>
<p>&nbsp;</p>
<br />
</p>
<p></p>
<p>&nbsp;</p>
</body>
</html>


and script:



<?PHP

define('db_name', 'webadmin_leaderboard');
define('db_user', 'webadmin');
define('db_password', 'collide198');
define('db_host', "localhost");

$link = mysql_connect(db_host, db_user, db_password);

if (!$link) {
die('Could Not Connect: ' . mysql_error());
}

$db_selected = mysql_select_db(db_name, $link);

if (!$db_selected) {
die('can\'t use' . db_name . ': ' . mysql_error());
}

echo 'Connected Successfully';



$name = $_POST[`Name`];

$points = $_POST[`Points`];

$sql = "INSERT INTO leaderboard (`name`,`points`) VALUES ('".$name."','".$points."')";


if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}

mysql_close();

?>

I also edited the table so that the id was primary, the name was unique and the points was blank.

still am having the same problem.

I am willing to give FTP access to anyone who can fix it.

I can also give temp access to the cpanel to adjust the database...

Really need this fixed. its driving me insane. i have followed all the steps on w3schools and various tut videos....

AHHH!!!

Wanna
09-16-2011, 11:09 AM
If you want you can send me the FTP data in a PM

Then i can help you

Also you could try this first:

Change this two lines


$name = $_POST[`Name`];

$points = $_POST[`Points`];

to


$name = $_POST['Name'];

$points = $_POST['Points'];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum