a little help please

Status
Not open for further replies.

NewFuture

New Member
Messages
1,658
Reaction score
0
Points
0
i am creating a submit high scores script kind of thing using php and mysql database, i have enclosed a step by step, i am currently on step 4, at the very bottom of the step, when i test my script i recieve error -4 (this will be all clear when you read step by step) therefore there is somthing wrong with my script, i have also enclosed my script sofar can anyone help me and tell me where i am going wrong? and how i can get it to work?

Step By Step:

http://www.peachpit.com/content/images/0321223659/files/Web_Lesson.pdf

My script sofar:

<?PHP
$name = $_POST['name'];
$score = $_POST['score'];
$link = @mysql_connect("localhost", "nwfuture_admin", "HIDDEN THE PASSWORD");
if (mysql_errno()){
exit("-2");
}
mysql_select_db('nwfuture_scoretest', $link);
if (mysql_errno()){
exit("-3");
}
$sql = "INSERT INTO scores(name, score) VALUES('$name', $score)";
$result = mysql_query($sql, $link);
if (mysql_errno()){
exit("-4");
}
exit("1");
?>
 

NewFuture

New Member
Messages
1,658
Reaction score
0
Points
0
extra: the error is caused by a problem with the last part of my script
 

n4tec

Active Member
Messages
3,312
Reaction score
0
Points
36
I have not gone through the code or tried if it works.. But your error was the @ before mysql_connect...

Try using the code below:-

PHP:
<?PHP
$name = $_POST['name'];
$score = $_POST['score'];
$link = mysql_connect("localhost", "nwfuture_admin", "HIDDEN THE PASSWORD");
if (mysql_errno()){
exit("-2");
}
$db_selected = mysql_select_db('nwfuture_scoretest', $link);
if (mysql_errno()){
exit("-3");
}
$sql = "INSERT INTO scores(name, score) VALUES('$name', $score)";
$result = mysql_query($sql, $link);
if (mysql_errno()){
exit("-4");
}
exit("1");
?>

try either the one above or the one below...
PHP:
<?PHP
$name = $_POST['name'];
$score = $_POST['score'];
$link = mysql_connect("localhost", "nwfuture_admin", "HIDDEN THE PASSWORD");
if (mysql_errno()){
exit("-2");
mysql_select_db('nwfuture_scoretest', $link);
if (mysql_errno()){
exit("-3");
}
$sql = "INSERT INTO scores(name, score) VALUES('$name', $score)";
$result = mysql_query($sql, $link);
if (mysql_errno()){
exit("-4");
}
exit("1");
?>

I used the link below as a reference..

http://www.php.net/mysql_connect
http://www.php.net/mysql_select_db
 

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
Or

Code:
<?PHP
$name = $_POST['name'];
$score = $_POST['score'];
$cfg['mysqlhost'] = 'localhost';	// Database host URL
$cfg['mysqluser'] = 'User';				// Database user
$cfg['mysqlpassword'] = 'Password';			// Database password
$cfg['mysqldb'] = 'Name';			// Database name
if (mysql_errno()){
exit("-2");
mysql_select_db('nwfuture_scoretest', $link);
if (mysql_errno()){
exit("-3");
}
$sql = "INSERT INTO scores(name, score) VALUES('$name', $score)";
$result = mysql_query($sql, $link);
if (mysql_errno()){
exit("-4");
}
exit("1");
?>
 

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
Or you can do this:

Save as error.php:
Code:
<html><head><title>Error!</title>
<link rel="stylesheet" type="text/css" href="[COLOR=DimGray]Path to stylesheet[/COLOR]" />
</head><body>
<table class="main_panel">
<tr><td>
<span class="game_name">Error!</span><br><br>
The information you have submitted is invalid.  There are three possibilities:<ul>
<li>There is a problem with the site.  (Very unlikely)</li>
<li>There is a problem with the game you're playing.  (More likely)</li>
<li>You're trying to cheat!  (Most likely!)</li></ul>
If you aren't trying to cheat, then please notifiy the author of the game (if possible) of this error.  If you are the author of the game, then please make a post in the [COLOR=Red]<a href="Your link">Site/Forum name</a>.[/COLOR]
</td></tr></table></body></html>

Save as hs_list.php:
Code:
<?
[COLOR=Red]$db = mysql_connect("Localhost", "Database Name", "Passwor");[/COLOR]
mysql_select_db("[COLOR=Red]Database name[/COLOR]", $db);
$sub=mysql_query("select * from site_hs_sub where sub_id='$sub_id'", $db);
if ($sub_info = mysql_fetch_array($sub)) {
        $sub_name = $sub_info["name"];
        $decimals = $sub_info["decimals"];
        $places = $sub_info["show_num"];
        $lowscore = $sub_info["lowscore"];
}
$game=mysql_query("select * from site_hs_game where game_id='$game_id'", $db);
if ($game_info = mysql_fetch_array($game)) {
        $game_name = $game_info["name"];
        $css_file = $game_info["css_file"];
}
$desc = "desc";
if ($lowscore == "Y") { $desc = ''; }
$query = "select * from site_hs_score where game_id='$game_id' and sub_id='$sub_id' order by score $desc limit 0, $places";
$scores = mysql_query($query, $db);
if ($s_row = mysql_fetch_array($scores)) {
        ?>
        <html><head>
        <link rel="stylesheet" type="text/css" href="[COLOR=Red]http://your_site.com/your_css_directory/[/COLOR]<? echo $css_file ?>" />
        <title><? echo $game_name ?> - <? echo $sub_name ?></title></head><body><center>
        <table class="main_panel">
        <tr><td>
        <div class="game_name"><? echo $game_name ?></div>
        <div class="sub_name"><? echo $sub_name ?></div><br>
        <?
        if (isset($rank)) {
                echo "<center>";
                echo "<span class=\"ranking_text\">Your Score: </span>";
                echo "<span class=\"ranking_num\">".number_format($score, 0)."</span> - ";
                echo "<span class=\"ranking_text\">Your Rank: </span>";
                echo "<span class=\"ranking_num\">".$rank."</span>";
                echo "</center>";
                echo "<br><br>";
        }
        else { $rank = -1; }
        ?>
        <table class="score_list" cellspacing=0>
        <?
        $i = 1;
        do {
                $st = $s_row["stamp"];
                $year = substr($st, 0, 4);
                $month = substr($st, 6, 2);
                $day = substr($st, 4, 2);
                $date = $day . " / " . $month . " / " . $year;
                if ($i % 2) { $p = 'odd'; } else { $p = 'even'; }
                if ($i == $rank) { $p = "new"; }
                ?>
                <tr class="row_<? echo $p ?>">
                <td class="rank"><? echo $i ?></td>
                <td class="player"><? echo $s_row["player"] ?></td>
                <td class="score"><? echo number_format($s_row["score"], $decimals) ?></td>
                <td class="date"><? echo $date ?></td>
                </tr>
                <?
                $i++;
        } while ($s_row = mysql_fetch_array($scores));
        ?>
        </table>
        </td></tr>
        </table>
        </center></body></html>
        <?
}
else { // no scores found
        ?>
        <html><head><title>No scores found</title></head>
        <body>
        No scores found.
        </body></html>
        <?
}

Save as hs_std.css:
Code:
* {
        font-family: sans-serif;
        padding: 0px;
        vertical-align: text-top;
        border-color: #000000;
        font-weight: bold;
}

body {
        margin: 15px;
        background-color: #3095CC;
}

td {
        padding-left: 10px;
        padding-right: 10px;
}

.main_panel {
        border-style: solid;
        border-width: 2px;
        border-color: #000000;
        padding: 20px;
        background-color: #ffffff;
}

.game_name {
        font-size: 175%;
        font-weight: bold;
        color: #00659C;
        text-align: center;
}

.sub_name {
        font-size: 150%;
        font-weight: bold;
        color: #00659C;
        text-align: center;
}

.ranking_text {
        font-weight: bold;
}

.ranking_num {
        font-weight: bold;
        color: #00659C;
}

.score_list {
        border-style: solid;
        border-width: 1px;
        border-color: #000000;
}

.score {
        text-align: right;
}

.rank {
        text-align: right;
        font-weight: bold;
}

tr.row_even {
        background-color: #D0D0D0;
}

tr.row_new {
        background-color: #FFA24A;
}

Theres more of the code. The Admins won't lemme post twice so quickly.
 

o0slowpaul0o

New Member
Messages
254
Reaction score
0
Points
0
Save as submit.php:
Code:
<?
function crc16($str) {
        $crc = 0;
        for ($i=0; $i<strlen($str); $i++) {
                $crc ^= ord(substr($str, $i, 1)) << 8;
                for ($j=0; $j<8; $j++) {
                        if ($crc & 0x8000) {
                                $crc = (($crc << 1) ^ 0x8005);
                        }
                        else { $crc <<= 1; }
                }
        }
        return $crc & 0xFFFF;
}

// get the password:
$db = mysql_connect("your_host_here", "your_database_name_here", "your_password_here");
mysql_select_db("your_database_name_here", $db);
$game_info = mysql_query("select * from site_hs_game where game_id = '$game_id'", $db);
$success = 0;
if ($g_row = mysql_fetch_array($game_info)) {
        $pwd = $g_row["password"];
        $game_name = $grow["name"];
        $calc = crc16($score . $player_name . $game_id . $sub_id . $pwd);
        if ($calc == $auth_code) {
                $sub = mysql_query("select * from site_hs_sub where game_id='$game_id' and sub_id='$sub_id'", $db);
                if ($sub_r = mysql_fetch_array($sub)) {
                        $sub_name = $sub_r["name"];
                        $lowscore = $sub_r["lowscore"];
                        if ($lowscore == 'Y') { $op = "<"; } else { $op = ">"; }
                        $exist = mysql_query("select * from site_hs_score where player='$player_name' and game_id='$game_id' and sub_id='$sub_id' and score=$score", $db);
                        if ($exists = mysql_fetch_array($exist)) {
                        }
                        else {
                                $insert = mysql_query("insert into site_hs_score (score, player, game_id, sub_id) values ($score, '$player_name', '$game_id', '$sub_id')", $db);
                        }
                        $query = "select count(*) as rank from site_hs_score where game_id='$game_id' and sub_id='$sub_id' and score $op $score";
                        //echo $query . "<br><br>";
                        $rank_q = mysql_query($query, $db);
                        $rank_r = mysql_fetch_array($rank_q);
                        $rank = $rank_r["rank"] + 1;
                }
                $success = 1;
        }
}
if ($success) {
        //echo "rank = " . $rank . " <br>";
        include("hs_list.php");
}
else {
        include("hs_submit_error.php");
}

?>

Then put this in PHPMyAdmin:
Code:
These are the table names and structures you'll need to setup in MySQL:




===============================================================================
Table: site_hs_game
===============================================================================
Field   	Type  			Attributes  	Null  	Default  	Extra  	
-------------------------------------------------------------------------------
id 	 		int(10) 		UNSIGNED 		No  	  			auto_increment  						
game_id  	varchar(6) 	  					No  	  	  						
name  		varchar(255) 	  				No  	  	  						
author  	varchar(64) 	  				No  	  	  						
homepage  	varchar(255) 	  				No  	  	  						
password  	varchar(6) 	  					No  	  	  						
css_file  	varchar(64) 	  				No  	std_hs.css  	  						
stamp  		timestamp(14) 	  				No  	
===============================================================================
This table has one record for each game.  The "game_id" field is the game_id
that must be given when submitting a score.  The "css_file" field holds the 
name of the CSS file to use when rendering the list.  You'll need to modify
the hs_list.php script to point to the directory on your site where you hold 
your CSS file(s).  You need to manually insert one row in this table for each 
game you want to track.
===============================================================================




===============================================================================
Table: site_hs_sub
===============================================================================
Field   	Type  			Attributes  	Null  	Default  	Extra  	
-------------------------------------------------------------------------------
uid   		int(6)  	   					No   	 		  	auto_increment   	    	    	    	    	    	
sub_id  	varchar(6) 	 				 	No  	  	  						
game_id  	varchar(6) 	 				 	No  	  	  						
name  		varchar(64) 				  	No  	  						
lowscore  	char(1) 	 				 	No  	N  	  						
show_num  	int(10) 		UNSIGNED	 	No  	50  	  						
decimals  	int(11) 	  					No  	0  	 
===============================================================================
This table has one record for each sublist.  The "game_id" field determines
which game the sublist belongs to.  The "sub_id" field is the sub_id that must
be given when submitting a score.  You need to manually insert one row for each
sublist that you want to track.
===============================================================================




===============================================================================
Table: site_hs_score
===============================================================================
Field   	Type  			Attributes  	Null  	Default  	Extra  	
-------------------------------------------------------------------------------
score_id   	int(3)  		UNSIGNED  		No   	   			auto_increment   	    	    	    	    	    	
score  		decimal(16,4) 				  	No  	  	  						
player  	varchar(20) 			  		No  	  	  						
game_id  	varchar(6) 	 				 	No  	  	  						
sub_id  	varchar(6) 					  	No  	  	  						
stamp  		timestamp(14)			 	  	No  	  	 
===============================================================================
This table has one record for each score submitted.  All you need to do is
create this table; you never need to insert records yourself.
===============================================================================
 

NewFuture

New Member
Messages
1,658
Reaction score
0
Points
0
nforchange said:
I have not gone through the code or tried if it works.. But your error was the @ before mysql_connect...

Try using the code below:-

PHP:
<?PHP
$name = $_POST['name'];
$score = $_POST['score'];
$link = mysql_connect("localhost", "nwfuture_admin", "HIDDEN THE PASSWORD");
if (mysql_errno()){
exit("-2");
}
$db_selected = mysql_select_db('nwfuture_scoretest', $link);
if (mysql_errno()){
exit("-3");
}
$sql = "INSERT INTO scores(name, score) VALUES('$name', $score)";
$result = mysql_query($sql, $link);
if (mysql_errno()){
exit("-4");
}
exit("1");
?>

try either the one above or the one below...
PHP:
<?PHP
$name = $_POST['name'];
$score = $_POST['score'];
$link = mysql_connect("localhost", "nwfuture_admin", "HIDDEN THE PASSWORD");
if (mysql_errno()){
exit("-2");
mysql_select_db('nwfuture_scoretest', $link);
if (mysql_errno()){
exit("-3");
}
$sql = "INSERT INTO scores(name, score) VALUES('$name', $score)";
$result = mysql_query($sql, $link);
if (mysql_errno()){
exit("-4");
}
exit("1");
?>

I used the link below as a reference..

http://www.php.net/mysql_connect
http://www.php.net/mysql_select_db


i tried both, the first one just gives me same error second one gives me this: Parse error: parse error, unexpected $ in /home/nwfuture/public_html/testing/scoresave.php on line 17
 

NewFuture

New Member
Messages
1,658
Reaction score
0
Points
0
note i made the databse name scoretest but when i had to type it in that script i had to use nwfuture_scoretest, hope this is any use and i have a idea do i have to change any permissions on that file ect?
 

NewFuture

New Member
Messages
1,658
Reaction score
0
Points
0
is there a database error cant connect to my shout box either www.newfuture.tk (shout box at bottom)
Warning: mysql_pconnect(): Access denied for user: 'nwfuture_newfutu@localhost' (Using password: YES) in /home/nwfuture/public_html/pabox/pabox.functions.php on line 20
paBox was unable to connect to the MySQL server
 

NewFuture

New Member
Messages
1,658
Reaction score
0
Points
0
yes i have, please see above thread^

here it is:

'is there a database error cant connect to my shout box either www.newfuture.tk (shout box at bottom)
Warning: mysql_pconnect(): Access denied for user: 'nwfuture_newfutu@localhost' (Using password: YES) in /home/nwfuture/public_html/pabox/pabox.functions.php on line 20
paBox was unable to connect to the MySQL server'
 

NewFuture

New Member
Messages
1,658
Reaction score
0
Points
0
NewFuture said:
yes i have, please see above thread^

here it is:

'is there a database error cant connect to my shout box either www.newfuture.tk (shout box at bottom)
Warning: mysql_pconnect(): Access denied for user: 'nwfuture_newfutu@localhost' (Using password: YES) in /home/nwfuture/public_html/pabox/pabox.functions.php on line 20
paBox was unable to connect to the MySQL server'

there is no problem for my shout box anymore, can anyone help me though with my high score script please, i get error:

Parse error: parse error, unexpected $ in /home/nwfuture/public_html/testing/scoresave.php on line 17
 

n4tec

Active Member
Messages
3,312
Reaction score
0
Points
36
try code below...

<?PHP
$name = $_POST['name'];
$score = $_POST['score'];
$link = @mysql_connect("localhost", "nwfuture_admin", "HIDDEN THE PASSWORD");
if (mysql_errno()){
exit("-2");
}
$db_selected = mysql_select_db('nwfuture_scoretest', $link);
if (mysql_errno()){
exit("-3");
}
$sql = "INSERT INTO scores(name, score) VALUES('$name', $score)";
$result = mysql_query($sql, $link);
if (mysql_errno()){
exit("-4");
}
exit("1");
?>
 
Status
Not open for further replies.
Top