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>
Tutorial Code is Buggy :)
Moderator: General Moderators
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
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
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$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
Code: Select all
// display individual record
if ( isset($_GET['id']) && !empty($id = $_GET['id']) )
{
$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);
...php4.2+ tutorial: feel free to post links if you find good ones, e.g. in viewtopic.php?t=2598
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>
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>
no, it because I'm posting trash today...
trythe shortcut I proposed is not possible with empty() 
try
Code: Select all
<?php
if ( isset($_GET['id']) && !empty($_GET['id']) )
{
$id = $_GET['id'];
...
}
?>