Web Analytics Made Easy -
StatCounter Going thru a tutorial and running into some problems - CodingForum

Announcement

Collapse
No announcement yet.

Going thru a tutorial and running into some problems

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Going thru a tutorial and running into some problems

    I was going thru a Tutorial some one had linked to on one of the post here but it seems to have errors and I was wondering if some one could take a look at it and give me a help fixing it. I found one problem and fixed it but still a few more. MySQL is set up fine.

    http://webmonkey.wired.com/webmonkey...tw=programming

    here's my code:
    Code:
    <html>
    <body>
      <?php
        $hostname_Con = "localhost";
        $database_Con = "mydb";
        $username_Con = "uid";
        $password_Con = "pword";
        $db = mysql_connect($hostname_Con, $username_Con, $password_Con) or die(mysql_error());
        mysql_select_db($database_Con,$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',position='$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 $PHP_SELF?>">ADD A RECORD</a>
          <P>
          <form method="post" action="<?php echo $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>

    Errors that I'm getting:
    Notice: Undefined variable: submit in c:\inetpub\wwwroot\test\AMD.php on line 10

    Notice: Undefined variable: delete in c:\inetpub\wwwroot\test\AMD.php on line 20

    Notice: Undefined variable: id in c:\inetpub\wwwroot\test\AMD.php on line 27

    Notice: Undefined variable: id in c:\inetpub\wwwroot\test\AMD.php on line 41

    and there is one thing in the code I don't understand and the tutorial doesn't explain (or I missed it) is on line 31

    Code:
    printf("<a href=\"%s?id=%s\">%s %s</a> \n", $_SERVER['PHP_SELF'], $myrow["id"], $myrow["first"], $myrow["last"]);
    What does the %s do
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #2
    Undefined variables are warnings, not errors, it means just as it says - you have variables that are not defined.
    The reason why you have generated and e_warning is simply because this script relys on register_globals set to on.
    Code:
        if ($submit) {
          // here if no ID then adding else we're editing
          if ($id) {
            $sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position' WHERE id=$id";
          } else {
            $sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
    The variables $submit, $id, $first, $last, $address, and $position do not exist globally. You need to alter these so they are taken from a request method - $_GET, $_POST or $_REQUEST superglobals depending on where its comming from ($_REQUEST is either post or get, where post overrides get).

    %s is taken as a string representation from the sprintf/printf functions, and according to this, its the order its pesented:
    PHP Code:
    printf("<a href=\"%s?id=%s\">%s %s</a> \n"$_SERVER['PHP_SELF'], $myrow["id"], $myrow["first"], $myrow["last"]); 
    Lets break it down:
    <a href="%s // This one will equal $_SERVER['PHP_SELF']
    ?id=%s // This one will equal $myrow['id']
    ">%s %s</a> // These two are $myrow['first'] and $myrow['last']
    Alternative formatting options can be found here
    Hope that helps.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment

    Working...
    X