Example.8-5.php help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JeanneHarris
Forum Newbie
Posts: 2
Joined: Sun Sep 05, 2010 9:57 am

Example.8-5.php help

Post by JeanneHarris »

I am new to php. I was working my way through the PHP and MySQL book by Hugh E. William and David Lane and I hit an issue that I can not figure out with Example.8-5.php .

When this line runs the $status comes back as empty.

$status = mysqlclean($_GET, "status", 1, $connection);

How can I debug this to see where the problem is?

Code: Select all

<?php
require "db.inc";
require_once "HTML/Template/ITX.php";

if (!($connection = @ mysql_connect("127.0.0.1", "root", "")))
   die("Could not connect to database");

$status = mysqlclean($_GET, "status", 1, $connection);

$template = new HTML_Template_ITX("./templates");
$template->loadTemplatefile("example.8-6.tpl", true, true);

switch ($status)
{
  case "T":
    $phonebook_id = mysqlclean($_GET, "phonebook_id", 5, $connection);

    if (!empty($phonebook_id))
    {
      if (!mysql_select_db("telephone", $connection))
         showerror();

      $query = "SELECT * FROM phonebook WHERE
                phonebook_id = {$phonebook_id}";

      if (!($result = @mysql_query ($query, $connection)))
        showerror();

      $row = @ mysql_fetch_array($result);

      $template->setCurrentBlock("success");
      $template->setVariable("SURNAME", $row["surname"]);
      $template->setVariable("FIRSTNAME", $row["firstname"]);
      $template->setVariable("PHONE", $row["phone"]);
      $template->parseCurrentBlock();
      break;
    }

  case "F":
    $template->setCurrentBlock("failure");
    $template->setVariable("MESSAGE", "A database error occurred.");
    $template->parseCurrentBlock();
    break;

  default:
    $template->setCurrentBlock("failure");
    $template->setVariable("MESSAGE", "You arrived here unexpectedly.");
    $template->parseCurrentBlock();
    break;
}

$template->show();
?>
mySQLClean

Code: Select all

<?php
   // This file is the same as example 6-7, but includes mysqlclean() and shellclean()

   $hostName = "127.0.0.1";
   //$databaseName = "winestore";
   $databaseName = "telephone";
   $username = "root";
   $password = "";

   function showerror()
   {
      print "Error, world";
      //die("Error " . mysql_errno() . " : " . mysql_error());
   }

   function mysqlclean($array, $index, $maxlength, $connection)
   {
     if (isset($array["{$index}"]))
     {
        $input = substr($array["{$index}"], 0, $maxlength);
        $input = mysql_real_escape_string($input, $connection);
        return ($input);
     }
     return NULL;
   }

   function shellclean($array, $index, $maxlength)
   {
     if (isset($array["{$index}"]))
     {
       $input = substr($array["{$index}"], 0, $maxlength);
       $input = EscapeShellArg($input);
       return ($input);
     }
     return NULL;
   }
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Example.8-5.php help

Post by Jonah Bron »

Code: Select all

if (isset($array["{$index}"]))
You passed $_GET as $array, and 'status' as $index. That would indicate that $_GET['status'] does not exist.
JeanneHarris
Forum Newbie
Posts: 2
Joined: Sun Sep 05, 2010 9:57 am

Re: Example.8-5.php help

Post by JeanneHarris »

I see... Thanks
Post Reply