|
-
Nov 7th, 2009, 03:21 PM
#1
Thread Starter
Member
[RESOLVED] Submitting scores
Hi,
I want to submit game scores to different MySql tables (tab1-tab2) according to the type of the game (1-4).
The PHP code reads the score, some more data and the type of game, stored in $gametype. then it goes through elseif statements to choose the right table.
However, something is not working in this code.
Can someone see a syntax error that I'm not able to see?
Thanks!
PHP Code:
<?
$username="******";
$password="******";
$database="******";
$gametype=0
// Connect to database
mysql_connect ("***********.com" , $username, $password) or die( "cannot connect" );
mysql_select_db($database) or die ("cannot open" );
//if(isset($_GET['name'], $_GET['score']))
if(isset($_GET['name'], $_GET['city'], $_GET['country'], $_GET['score'], $_GET['date'], $_GET['gametype']))
{
$name = mysql_real_escape_string($_GET['name']);
$city = mysql_real_escape_string($_GET['city']);
$country = mysql_real_escape_string($_GET['country']);
$score = (int) $_GET['score'];
$date = mysql_real_escape_string($_GET['date']);
$gametype = (int) $_GET['gametype']);
print ($gametype);
if ($gametype == 1) {
$result = mysql_query ("INSERT INTO tab1 (Name,City,Country,Score,Date) VALUES ('$name','$city','$country','$score','$date')");
} elseif ($gametype == 2) {
$result = mysql_query ("INSERT INTO tab2 (Name,City,Country,Score,Date) VALUES ('$name','$city','$country','$score','$date')");
} elseif ($gametype == 3) {
$result = mysql_query ("INSERT INTO tab3 (Name,City,Country,Score,Date) VALUES ('$name','$city','$country','$score','$date')");
} elseif ($gametype == 4) {
$result = mysql_query ("INSERT INTO tab4 (Name,City,Country,Score,Date) VALUES ('$name','$city','$country','$score','$date')");
}
if($result)
echo 'score submitted!';
mysql_close();
?>
-
Nov 7th, 2009, 03:31 PM
#2
Re: Submitting scores
you have an extra bracket in your code on the end of the following line:
PHP Code:
$gametype = (int) $_GET['gametype']);
in the future, please post any and all errors you get. make a call to error_reporting(E_ALL) at the beginning of your script if you're not getting any.
anyway, you could clean up your code a little bit -- this seems like something a little better to maintain:
PHP Code:
switch($gametype){ /* our supported game types */ case 1: case 2: case 3: case 4: $table = "tab" . $gametype; break; default: /* unsupported/invalid game type */ echo "Unsupported game type!"; }
if(isset($table)){ $result = mysql_query ("INSERT INTO {$table} (Name,City,Country,Score,Date) VALUES ('$name','$city','$country','$score','$date')"); }
if($result){ echo "Score submitted!"; }
now, if you end up having more "game types," you only need to add a case to the switch statement, rather than add another if-else structure.
and, lastly, you need not use mysql_close(). PHP will close your MySQL connection for you.
edit: I just noticed that you also have a missing terminator (semi-colon, ";") on the end of this line at the beginning of your script:
Last edited by kows; Nov 7th, 2009 at 03:35 PM.
-
Nov 8th, 2009, 03:59 AM
#3
Thread Starter
Member
Re: Submitting scores
kows, thanks a lot for you help!
-
Nov 9th, 2009, 07:10 AM
#4
Thread Starter
Member
Re: [RESOLVED] Submitting scores
OK, yet another problem. Now I'm trying to displaing the MySQL table data on an HTML page, where Name, City, Country, Score and Date are table entries, and name, city, country, score, date are variables:
PHP Code:
<html>
<body>
<?php
$username="******";
$password="******";
$database="******";
mysql_connect("*********.com" ,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tab1";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">City</font></th>
<th><font face="Arial, Helvetica, sans-serif">Country</font></th>
<th><font face="Arial, Helvetica, sans-serif">Score</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date</font></th>
</tr>
<?php
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"Name");
$city=mysql_result($result,$i,"City");
$country=mysql_result($result,$i,"Country");
$score=mysql_result($result,$i,"Score");
$date=mysql_result($result,$i,"Date");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $city; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $country; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $score; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $date; ?></font></td>
</tr>
<?php
$i++;
}
?>
</body>
</html>
I only gate a page with an empty table and I can't figure out what's wrong with the script.
You help is appreciated!
-
Nov 9th, 2009, 11:02 AM
#5
Thread Starter
Member
Re: [RESOLVED] Submitting scores
False alarm. 
I forgot to upload the file to the server... Sorry!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|