Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2004, 04:24 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pls Help

newbie trying to learn php...

i have just install php on my pc using iis...
i was able to show the info(phpinfo()).
but when i try to run come of the scripts to tried out it keeps giving me error..
the easy helloworld.php works out fine.
i was able to retrieve some other pages from mysql, but i just don't understand whats wrong with this code..

pls see error message.

Notice: Undefined variable: id in c:\inetpub\wwwroot\php\smart1.php on line 9

Notice: Undefined variable: PHP_SELF in c:\inetpub\wwwroot\php\smart1.php on line 31
Bob Smith

Notice: Undefined variable: PHP_SELF in c:\inetpub\wwwroot\php\smart1.php on line 31
John Roberts

Notice: Undefined variable: PHP_SELF in c:\inetpub\wwwroot\php\smart1.php on line 31
Brad Johnson

attach is code..


<html>
<body>

<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);

if ($id) {
  // query the DB
  $sql = "SELECT * FROM employees WHERE id=$id";
  $result = mysql_query($sql);
  $myrow = mysql_fetch_array($result);

?>
  <form method="post" action="<?php echo $PHP_SELF?>">
  <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
  First name:<input type="Text" name="first" value="<?php echo $myrow["first"] ?>"><br>
  Last name:<input type="Text" name="last" value="<?php echo $myrow["last"] ?>"><br>
  Address:<input type="Text" name="address" value="<?php echo $myrow["address"] ?>"><br>
  Position:<input type="Text" name="position" value="<?php echo $myrow["position"] ?>"><br>
  <input type="Submit" name="submit" value="Enter information">
  </form>

<?php
} else {
  // display list of employees
  $result = mysql_query("SELECT * FROM employees",$db);

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

</body>
</html>
 
Old March 29th, 2004, 04:26 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default


http://p2p.wrox.com/archive/beginnin...2002-11/17.asp
http://www.google.com/search?q=regis...e:p2p.wrox.com


The problem is that $id doesn't exist because PHP isn't configured to copy variables submitted by GET or POST forms into global variables. Read the above links for more info about the issue and how to fix your script.


Take care,

Nik
http://www.bigaction.org/
 
Old April 22nd, 2004, 12:47 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i must be real dumb cos it is still giving me problems after following ur instruction...
anyway, i got the book begining php4 and i follow all the instructions closely...
except tat i am using IIS
(Btw, using iis does not need to have apache rite? cos tats wat i thot, unless otherwise)

i tried the codes from chapter 3 (checkbox.html, checkbox.php, checkboxes.html & checkboxes.php)
it all gives me the erroe undefine variable.

so i change the codes base on wat u have said, thru my understanding, and it does not show anything after that.
Its a empty screen after i click submit from checkbox.html and checkboxes.html.

checkbox.html
===============
<HTML>
<HEAD></HEAD>
<BODY>
<FORM METHOD=POST ACTION="checkbox.php">
Have you ever eaten haggis before?<INPUT NAME="Choice" TYPE="Checkbox">
<BR><BR><INPUT TYPE=SUBMIT>
</FORM>
</BODY></HTML>

checkbox.php
============
<HTML>
<HEAD></HEAD>
<BODY>
<?php
$Choice = & $_GET['Choice'];
echo $Choice;
?>
</BODY></HTML>

checkboxes.html
===============
<HTML>
<HEAD></HEAD>
<BODY>
<?php
$Choice1 = & $_GET['Choice1'];
$Choice2 = & $_GET['Choice2'];
$Choice3 = & $_GET['Choice3'];

echo "$Choice1<BR>";
echo "$Choice2<BR>";
echo "$Choice3<BR>";
?>
</BODY>
</HTML>

please guide me along.
thanks
 
Old April 22nd, 2004, 01:01 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Here's one problem, you haven't given the checkbox field a value, so your script won't output anything. Second, you are using the $_GET superglobal to access 'Choice' when your HTML form is sending via the POST method, change one or the other.

checkbox.html
===============
<HTML>
<HEAD></HEAD>
<BODY>
<FORM METHOD=POST ACTION="checkbox.php">
Have you ever eaten haggis before?<INPUT NAME="Choice" TYPE="Checkbox" value='yes'>
<BR><BR><INPUT TYPE=SUBMIT>
</FORM>
</BODY></HTML>

Here you're passing the value of $_GET['Choice'] to $Choice by reference (that's what the amphersand does in the assignment). Why not just echo the orginal value and skip the extra step. Second, you aren't using any validation to check that the value of this variable exists.

Code:
// Original Code
checkbox.php
============
<HTML>
<HEAD></HEAD>
<BODY>
<?php
$Choice =& $_GET['Choice'];
echo $Choice;
?>
</BODY></HTML>
Since you're using the POST method in your HTML form, we'll access the variable via the POST superglobal array, and we'll use a little validation to make sure a value exists in the first place.

Code:
// Modified
<HTML>
<HEAD></HEAD>
<BODY>
<?php
     if (isset($_POST['Choice']) && !empty($_POST['Choice']))
     {
          echo $_POST['Choice'];
     }
     else
     {
          echo 'You did not enter a value!';
     }
?>
</BODY></HTML>
hth,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old April 22nd, 2004, 01:43 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi richard,

Thank you for answering my post.
I got the codes from the books. Begining PHP4 by wrox ISBN : 1-861003-73-0
borrow it from the library as i can't find 1 around my neighbourhood.
Does tat means all the codes can't work without some modifications?

I did not do any validations becos i was trying out whether it will work as i was having some configurations problems earlier.
 
Old April 22nd, 2004, 01:53 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You should be able to get the code to work by turning register_globals on and setting error_reporting to E_ALL & ~E_NOTICE. Bear in mind that it's not considered good style (anymore) to write code that depends on these settings, but if it helps you to start learning the language, then by all means do it!

When you're more comfortable programming in PHP, it should be much easier to go back to code you've already written and identify what needs to be changed to make it work without register_globals and the strictest error_reporting (E_ALL).


Take care,

Nik
http://www.bigaction.org/
 
Old April 23rd, 2004, 03:15 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank u so much...
i am able to get the code to work out fine now.

can u point me to a website or book that teaches the new coding practise.
i would prefer to learn the newer method rather than to do catch up later, but for now i will just try to understand all the examples tat is in the book.

is there an index or list as to wat is new in php version 4?
 
Old April 23rd, 2004, 03:44 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can look at PHP's change log, though I don't know how useful it will be...
  http://www.php.net/changelog



Take care,

Nik
http://www.bigaction.org/




Similar Threads
Thread Thread Starter Forum Replies Last Post
pls help me.. nilusharief003 ASP.NET 2.0 Professional 5 December 4th, 2008 08:05 AM
Pls help me!! hoailing22 ASP.NET 1.0 and 1.1 Basics 4 June 10th, 2005 11:24 PM
Pls help me hoailing22 ASP.NET 1.0 and 1.1 Basics 3 May 24th, 2005 11:08 AM
Pls!!! I need your Help!!! Ariel ASP.NET 1.0 and 1.1 Basics 3 May 9th, 2005 06:34 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.