Page 1 of 1

Tutorial Code is Buggy :)

Posted: Thu May 15, 2003 5:38 pm
by fyounus99
I am receiving the following error when I run the code shown below. I am confused how this code could be producing errors since I obtained it from a Web Monkey Tutorial.

URL: http://hotwired.lycos.com/webmonkey/99/ ... rogramming

I am running PHP Version 4.3.1 on a Windows Machine

Please help I love php and would like to learn how to use it.

Thank You.


Error
---------------------------------------------------------------
Notice: Undefined variable: id in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 15

Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41
Bob Smith

Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41
John Roberts

Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41
Brad Johnson

Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41


Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41


Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41

---------------------------------------
code
---------------------------------------

<html>

<body>

<?php



$db = mysql_connect("localhost", "root");

mysql_select_db("mydb",$db);

// display individual record

if ($id) {

$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);

$myrow = mysql_fetch_array($result);

printf("First name: %s\n<br>", $myrow["first"]);

printf("Last name: %s\n<br>", $myrow["last"]);

printf("Address: %s\n<br>", $myrow["address"]);

printf("Position: %s\n<br>", $myrow["position"]);

} else {

// show employee list

$result = mysql_query("SELECT * FROM employees",$db);

if ($myrow = mysql_fetch_array($result)) {

// display list if there are records to display

do {

printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);

} while ($myrow = mysql_fetch_array($result));

} else {

// no records to display

echo "Sorry, no records were found!";

}

}



?>



</body>



</html>

Posted: Thu May 15, 2003 5:42 pm
by volka

Posted: Fri May 16, 2003 10:35 am
by fyounus99
Jason thanks for the help. I was able to incorporate your code, however it is still producing an error. Please help. Also are you aware of any tuturials that are for php 4.3.1 on the windows platform. This might be easier for me to begin with.

Thanks Again.


code
----------------

<html>

<body>

<?php



$db = mysql_connect("localhost", "root");

mysql_select_db("mydb",$db);

// display individual record
$id = $_GET['id'];
if ($id) {

$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);

$myrow = mysql_fetch_array($result);

printf("First name: %s\n<br>", $myrow["first"]);

printf("Last name: %s\n<br>", $myrow["last"]);

printf("Address: %s\n<br>", $myrow["address"]);

printf("Position: %s\n<br>", $myrow["position"]);

} else {

// show employee list

$result = mysql_query("SELECT * FROM employees",$db);

if ($myrow = mysql_fetch_array($result)) {

// display list if there are records to display

do {

printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);

} while ($myrow = mysql_fetch_array($result));

} else {

// no records to display

echo "Sorry, no records were found!";

}

}



?>



</body>



</html>

----------------
error
-------------------



Notice: Undefined index: id in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 14

Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41
Bob Smith

Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41
John Roberts

Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41
Brad Johnson

Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41


Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41


Notice: Undefined variable: PHP_SELF in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 41

Posted: Fri May 16, 2003 11:50 am
by volka
if the script is called without parameter id there is no element $_GET['id']. That's what php is moaning about, it's only a notice/warning but to get rid of it, test the existence of this parameter

Code: Select all

// display individual record
if ( isset($_GET['id']) && !empty($id = $_GET['id']) )
{
	$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db); 
	...
$PHP_SELF is deprecated (and not defined with register_globals=Off), use $_SERVER['PHP_SELF'] instead (see: http://www.php.net/manual/en/reserved.variables.php)


php4.2+ tutorial: feel free to post links if you find good ones, e.g. in viewtopic.php?t=2598

Posted: Fri May 16, 2003 1:09 pm
by fyounus99
Thanks for the help volka. When I post your code this is what is receive.
Please let me know if I missed something.

Thank You-

error
-----

Parse error: parse error, unexpected '=', expecting ')' in C:\Documents and Settings\Administrator\My Documents\Inetpub\wwwroot\php\p\lesson2.php on line 16

error code
-----
line 16:
if ( isset($_GET['id']) && !empty($id = $_GET['id']) ) {

entire code
-----

<html>

<body>

<?php



$db = mysql_connect("localhost", "root");

mysql_select_db("mydb",$db);

// display individual record

//if ($id) {
if ( isset($_GET['id']) && !empty($id = $_GET['id']) ) {


$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);

$myrow = mysql_fetch_array($result);

printf("First name: %s\n<br>", $myrow["first"]);

printf("Last name: %s\n<br>", $myrow["last"]);

printf("Address: %s\n<br>", $myrow["address"]);

printf("Position: %s\n<br>", $myrow["position"]);

} else {

// show employee list

$result = mysql_query("SELECT * FROM employees",$db);

if ($myrow = mysql_fetch_array($result)) {

// display list if there are records to display

do {

printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"], $myrow["last"]);

} while ($myrow = mysql_fetch_array($result));

} else {

// no records to display

echo "Sorry, no records were found!";

}

}



?>



</body>



</html>

Posted: Fri May 16, 2003 1:29 pm
by volka
no, it because I'm posting trash today...
try

Code: Select all

<?php
if ( isset($_GET['id']) && !empty($_GET['id']) )
{ 
	$id = $_GET['id'];
	...
}
?>
the shortcut I proposed is not possible with empty() :?

Posted: Fri May 16, 2003 2:15 pm
by fyounus99
Thank You volka. It worked !!! :D