[HELP]Undefined Varible

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

[HELP]Undefined Varible

Post by bla5e »

code (note starting at line 23):

Code: Select all

<?php 

error_reporting(E_ALL);  
switch ($id) {
	default:
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY name";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'cdname':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY cdname";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'genre':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY genre";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'tracks':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>")  or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY tracks";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'rlsdate':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY rlsdate";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;
	
	case 'rlsgroup':
		$dbh=mysql_connect ("localhost", "<hidden>", "<hidden>") or die ('I cannot connect to the database because: ' . mysql_error()); 
		mysql_select_db ("cds");
		$sql = "SELECT * FROM cds ORDER BY rlsgroup";
		echo mysql_error();
		$result = mysql_query($sql, $dbh);
		echo mysql_error();
	break;

}
---


Errors:
Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 36

Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 45

Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 54

Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 63

Notice: Undefined variable: id in c:\program files\apache group\apache\htdocs\cdview.php on line 72
----

Line 36:

Code: Select all

case 'cdname':
Line 45:

Code: Select all

case 'genre':
Line 54:

Code: Select all

case 'tracks':
Line 63:

Code: Select all

case 'rlsdate':
Line 72:

Code: Select all

case 'rlsgroup':
-------------------------

I ran the script thro Zend Dev Environment, but it didnt pickup anything - do you guys see any problems with the script?

Edit: when i run the script like "index.php?id=genre" it wont display the errors
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Those arn't errors just notices, saying you havn't defined the variable before you are checking it.
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Post by bla5e »

Phenom wrote:Those arn't errors just notices, saying you havn't defined the variable before you are checking it.
how do i make them go away :-z
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make sure the variable is set...

Code: Select all

$id = isset($id) ? $id : '';

btw, you can move all but the $sql setting lines out of those cases and place them around the switch call....
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

I can't even see the need for a switch at all as there's too much replicated code, i'd just do something like:

Code: Select all

<?php
$valid = array(
  'name', 'cdname', 'genre', 'tracks', 'rlsdate', 'rlsgroup'
);
$id = empty($_GET['id']) ? 'name' : $_GET['id'];
if(in_array($id, $valid)){
  mysql_connect ("localhost", "<hidden>", "<hidden>") or die(mysql_error());
  mysql_select_db ('cds') or die(mysql_error());
  $sql = 'SELECT * FROM cds ORDER BY '.$id;
  $result = mysql_query($sql) or die(mysql_error());
} else {
  echo 'Invalid id: '.$_GET['id'];
}
?>
bla5e
Forum Contributor
Posts: 234
Joined: Tue May 25, 2004 4:28 pm

Post by bla5e »

markl999 wrote:I can't even see the need for a switch at all as there's too much replicated code, i'd just do something like:

Code: Select all

<?php
$valid = array(
  'name', 'cdname', 'genre', 'tracks', 'rlsdate', 'rlsgroup'
);
$id = empty($_GET['id']) ? 'name' : $_GET['id'];
if(in_array($id, $valid)){
  mysql_connect ("localhost", "<hidden>", "<hidden>") or die(mysql_error());
  mysql_select_db ('cds') or die(mysql_error());
  $sql = 'SELECT * FROM cds ORDER BY '.$id;
  $result = mysql_query($sql) or die(mysql_error());
} else {
  echo 'Invalid id: '.$_GET['id'];
}
?>
thanks
Post Reply