Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-23-2012, 04:08 AM   PM User | #1
KazeFlame
New Coder

 
Join Date: Sep 2012
Location: Philippines
Posts: 11
Thanks: 9
Thanked 0 Times in 0 Posts
KazeFlame is an unknown quantity at this point
Question [HELP]Undefined index Error

Error:
Code:
Notice: Undefined index: gameid in C:\Xampp\htdocs\index.php on line 5
Code:
PHP Code:
<?php

include 'connect.php';

$id $_POST['gameid'];
$step1 "select * from flashgames where gameid = '$id'";
$step2 mysql_query($step1) or die ("Could not select game");
$flashgame mysql_fetch_array($step2);

echo 
"<center>";
echo 
"<h1>" $flashgame['name'] . "</h1>";
echo 
"<embed src='" $flashgame['url'] . "' width='750' height='480'></embed>";
echo 
"<p>Game played " $flashgame['playedtimes'] . " times.</p>";
echo 
"<b>Game Desciption:</b><br/>" $flashgame['description'];
echo 
"<br/><b>Genre:</b> " $flashgame['genre'];
echo 
"<br/><b>Controls:</b><br/>" $flashgame['controls'];
echo 
"<br/><br/>Did you like the game? <!-- Node.js goes here soon --><form action='rate.php?gameid=1' method='get'><input type='button' name='rate' value='YES'/><input type='button' name='rate' value='NO'/></form>";
echo 
"YES: " $flashgame['rate-yes'] . "%<br/>NO: " $flashgame['rate-no'] . "%";
echo 
"</center>";

?>
For those who will help.
KazeFlame is offline   Reply With Quote
Old 10-23-2012, 08:21 AM   PM User | #2
davidjones1990
New Coder

 
Join Date: Sep 2011
Posts: 22
Thanks: 0
Thanked 3 Times in 3 Posts
davidjones1990 is an unknown quantity at this point
It basically means that gameid isn't in the $_POST array.

Try check of its set before trying to use it.
davidjones1990 is offline   Reply With Quote
Users who have thanked davidjones1990 for this post:
KazeFlame (10-23-2012)
Old 10-23-2012, 09:22 AM   PM User | #3
idalatob
Regular Coder

 
Join Date: Sep 2007
Location: Grahamstown, South Africa
Posts: 237
Thanks: 6
Thanked 17 Times in 17 Posts
idalatob is on a distinguished road
Couple of small tips:

PHP Code:
//use 'isset' to determine if a variable exists
if (!isset($_POST['gameid'])) {
    die(
"No game defined");
}

//escape any content you may be getting from the user
//otherwise, you are putting your website in danger (google -> mysql injection)

$id mysql_real_escape_string($_POST['gameid']); 
There is a really nice sticky on this forum somewhere (i'll go look for the link), that details good practice when writing PHP code.

Edit: Found the link, here it is: http://www.codingforums.com/showthread.php?t=220807
idalatob is offline   Reply With Quote
Users who have thanked idalatob for this post:
KazeFlame (10-23-2012)
Old 10-23-2012, 11:38 AM   PM User | #4
KazeFlame
New Coder

 
Join Date: Sep 2012
Location: Philippines
Posts: 11
Thanks: 9
Thanked 0 Times in 0 Posts
KazeFlame is an unknown quantity at this point
XD
Problem solve. I wrote POST instead of GET.
KazeFlame is offline   Reply With Quote
Old 10-23-2012, 04:26 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by KazeFlame View Post
XD
Problem solve. I wrote POST instead of GET.
This still won't actually solve the problem. The issue is you are accessing array offsets which may or may not exist. PHP triggers a notice when it does not, but you code will happily continue without a set variable. If you access the page directly, than it will not work properly.
As pointed out, you need to check that its set before doing any processing.
PHP Code:
if (isset($_GET['gameid']))
{
    
// all your code in here
}
else
{
    print 
'No data to show.';

The code you have doesn't verify that there isn't a problem otherwise. If the value isn't set, then you still end up with the results, but will trigger many errors since $flashgame will be null. The die on the query won't do anything unless its syntactically a failure; querying an invalid where isn't considered a failure, it will simply return a resultset with no results in it.

Quote:
Originally Posted by idalatob View Post
Couple of small tips:

PHP Code:
//use 'isset' to determine if a variable exists
if (!isset($_POST['gameid'])) {
    die(
"No game defined");
}

//escape any content you may be getting from the user
//otherwise, you are putting your website in danger (google -> mysql injection)

$id mysql_real_escape_string($_POST['gameid']); 
There is a really nice sticky on this forum somewhere (i'll go look for the link), that details good practice when writing PHP code.

Edit: Found the link, here it is: http://www.codingforums.com/showthread.php?t=220807
Its a little out of date, but many of the same concepts still applies. I'd recommend moving to PDO or MySQLi even just for the prepared statements. Statements do not need to be escaped since the sql structure is compiled separately from the data provided. So all you need to do in advance is make sure that magic_quotes_gpc isn't running (gone as of 5.4 btw), and if it is to issue a stripslashes first.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
KazeFlame (10-25-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:43 AM.


Advertisement
Log in to turn off these ads.