Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
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 September 8th, 2003, 01:33 PM
Authorized User
 
Join Date: Sep 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default undefined variables

I get the error message undefined variables for $id, $submit, and $delete. I also get the following error message for each input field:
<br /><b>Notice</b>: Undefined variable: first in <b>c:\phpfiles\finaldatabase.php</b> on line <b>118</b><br />
Database fields are: id, first,last, address, and possition. Any help would be appreciated.
with the following code:

<?php
$db = mysql_connect("localhost", "root","hal3000");
mysql_select_db("mydb",$db);
if ($submit) {
  // here if no ID then adding else we're editing
  if ($id) {
    $sql = "UPDATE employees SET first='$first',last='$last',address='$address',pos ition='$position' WHERE id=$id";
  } else {
    $sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
  }
  // run SQL against the DB
  $result = mysql_query($sql);
  echo "Record updated/edited!<p>";
} elseif ($delete) {
    // delete a record
    $sql = "DELETE FROM employees WHERE id=$id";
    $result = mysql_query($sql);
    echo "$sql Record deleted!<p>";
} else {
  // this part happens if we don't press submit
  if (!$id) {
    // print the list if there is not editing
    $result = mysql_query("SELECT * FROM employees",$db);
    while ($myrow = mysql_fetch_array($result)) {
      printf("<a href=\"%s?id=%s\">%s %s</a> \n", $_SERVER['PHP_SELF'], $myrow["id"], $myrow["first"], $myrow["last"]);
      printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $_SERVER['PHP_SELF'], $myrow["id"]);
    }
  }
  ?>

  <P>

  <a href="<?php echo $_SERVER['PHP_SELF']?>">ADD A RECORD</a>

  <P>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">

  <?php



  if ($id) {

    // editing so select a record

    $sql = "SELECT * FROM employees WHERE id=$id";

    $result = mysql_query($sql);

    $myrow = mysql_fetch_array($result);

    $id = $myrow["id"];

    $first = $myrow["first"];

    $last = $myrow["last"];

    $address = $myrow["address"];

    $position = $myrow["position"];

    // print the id for editing



    ?>

    <input type=hidden name="id" value="<?php echo $id ?>">

    <?php

  }



  ?>

  First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>

  Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br>

  Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>

  Position:<input type="Text" name="position" value="<?php echo $position ?>"><br>

  <input type="Submit" name="submit" value="Enter information">

  </form>



<?php



}



?>



</body>

</html>


 
Old September 8th, 2003, 02:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default


You're problems are due to your error_reporting and register_globals settings in php.ini.

Check the following links for more information:

  http://p2p.wrox.com/archive/beginnin...2002-11/17.asp
  http://p2p.wrox.com/archive/beginnin...2003-03/14.asp

  http://www.php.net/security
  http://www.php.net/security.registerglobals
  http://www.php.net/security.variables


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined Varriable syedferhat Beginning PHP 1 December 26th, 2007 04:41 PM
undefined Adam H-W Javascript 5 September 26th, 2007 01:31 AM
XSLTC compiler error about undefined variables cox1019823 XSLT 1 May 17th, 2006 08:50 AM
undefined object shoakat Classic ASP Databases 3 November 4th, 2004 05:46 PM
Undefined function dhborchardt Classic ASP Databases 0 June 7th, 2004 03:31 PM





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