Page 1 of 1

[SOLVED] upgraded now code won't work part 2

Posted: Sat Jan 08, 2005 5:56 pm
by surban99
feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


code

Code: Select all

<html>

<body>

<?php



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

mysql_select_db("test",$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["name"]);

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

  
} 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</a><br>\n", $_SERVER['PHP_SELF'], $myrow["id"], $myrow["name"]);

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

    } else {

      // no records to display

      echo "Sorry, no records were found!";	

    }

}



?>



</body>



</html>
error message:
Notice: Undefined variable: id in c:\program files\easyphp\www\example5.php on line 15
a little what the heck happened might be helpful to me as well


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Jan 08, 2005 5:58 pm
by markl999
Looks like your old code relies on register_globals being On and recent versions of PHP have it set to Off by default for good reasons ;)
So rather than use $id (which won't be set) you need to use $_GET['id']
Same goes for form posts too, use $_POST['id']

So basically, where ever you are using $id use $_GET['id'] or do:
$id = $_GET['id']; before you attempt to use $id elsewhere.

There's some validation etc you should be doing as 'id' is a user submitted value but that's another subject ;)

Posted: Sat Jan 08, 2005 6:04 pm
by surban99
did that, now i get
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in c:\program files\easyphp\www\example5.php on line 17
ps. did you ever know that your my hero?

Posted: Sat Jan 08, 2005 6:06 pm
by markl999
I'm guessing (helps to post the line containing the error ;)) you've done:

Code: Select all

$result = mysql_query("SELECT * FROM employees WHERE id=$_GET['id']",$db);
?
If so, that needs to be like the below (any one of the below will do):

Code: Select all

$result = mysql_query("SELECT * FROM employees WHERE id={$_GET['id']}",$db);
$result = mysql_query("SELECT * FROM employees WHERE id=".$_GET['id'],$db);

Posted: Sat Jan 08, 2005 6:40 pm
by surban99
i would declare this one solved

Posted: Sat Jan 08, 2005 7:26 pm
by feyd
I'm getting very tired of editing your posts to use

Code: Select all

tags, please start using them.

Posted: Sat Jan 08, 2005 10:51 pm
by surban99
my sincerest apologies