Jump to content

Recommended Posts

I am attempting to follow along in an example in a book ("PHP and MySQL" by O'Reilly, ISBN-13: 978-0-596-000543-1).

 

The example's point is to add info from a web form to a database while preventing a reload of the webpage from duplicating the item in that database and assigning the item a unique primary key with the auto_increment feature of MySQL.

 

There is a main php script (example.8-4.php) that calls a second (example.8-5.php) and uses a third template file living in ./templates in the webroot (example.8-6.tpl).

 

I have the directives:

 

error_reporting E_ALL
display_errors = 1

 

set in my php.ini file. I have also removed the error suppression ('@') from the mysql commands in an effort to determine the exact error. However all that happens when I access the example.8-4.php script, is I get an error page displayed that shows me message 'A database error occurred.' It seems that new eyes are needed at this point and any help rendered would be appreciated.

 

example.8-4.php

<?php
require "db.inc";

// Test for user input
if (!empty($_GET["surname"]) &&
    !empty($_GET["firstname"]) &&
    !empty($_GET["phone"]))
{
  if (!($connection =  mysql_connect("localhost", "thatguy", "yahright?")))
     die("Could not connect to database");

  $surname = mysqlclean($_GET, "surname", 50, $connection);
  $firstname = mysqlclean($_GET, "firstname", 50, $connection);
  $phone = mysqlclean($_GET, "phone", 20, $connection);

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

  // Insert the new phonebook entry
  $query = "INSERT INTO phonebook VALUES
            (NULL, '{$surname}', '{$firstname}', '{$phone}')";

  if (@mysql_query ($query, $connection))
  {
    header("Location: example.8-5.php?status=T&" .
           "phonebook_id=". mysql_insert_id($connection));
    exit;
  }
} // if empty()

header("Location: example.8-5.php?status=F");
?>

 

example.8-5.php

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

if (!($connection =  mysql_connect("localhost", "thatguy", "yahright?")))
   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();
?>

 

example.8-6.tpl inside of ./templates

<!DOCTYPE HTML PUBLIC
                 "-//W3C//DTD HTML 4.01 Transitional//EN"
                 "http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Phonebook Entry Receipt</title>
</head>
<body>
<!-- BEGIN success -->
<h1>Added a Phonebook Entry</h1>
<table>
<tr>
  <td>Surname:
  <td>{SURNAME}
</tr>
<tr>
  <td>First name:
  <td>{FIRSTNAME}
</tr>
<tr>
  <td>Phone number:
  <td>{PHONE}
</tr>
</table>
<!-- END success -->
<!-- BEGIN failure -->
<h1>{MESSAGE}</h1>
<!-- END failure -->
</body>
</html>

 

All of this is accessing a VERY simple database called 'telephone' and consists of this one easy table:

 

CREATE TABLE phonebook ( phonebook_id int(6) NOT NULL auto_increment, surname CHAR(50) NOT NULL, firstname CHAR(50) NOT NULL, phone CHAR(20) NOT NULL, PRIMARY_KEY (phonebook_id) ) type=MyISAM;

 

 

Link to comment
https://forums.phpfreaks.com/topic/184259-mysterious-database-error/
Share on other sites

Good God, that code from your book is a joke.

 

You should perform error reporting at the point where the error was detected. By redirecting all over the place, you are not carrying over the information about what caused the error.

 

These are the lines from the end of the first code you posted -

 

  if (@mysql_query ($query, $connection))
  {
    header("Location: example.8-5.php?status=T&" .
           "phonebook_id=". mysql_insert_id($connection));
    exit;
  }
} // if empty()

header("Location: example.8-5.php?status=F");
?>

 

Change them to this -

 

  if (mysql_query ($query, $connection))
  {
    header("Location: example.8-5.php?status=T&" .
           "phonebook_id=". mysql_insert_id($connection));
    exit;
  } else {
        // the query failed, do some basic error reporting -
       echo mysql_error();
  }
} // if empty()
?>

 

OK, thanks and duly noted.  8)

 

The code has been changed to the following with NO change to the result.  :shrug:

 

Still no detailed error and with the same cryptic error message.

 

example.8-4.php

<?php
require "db.inc";

// Test for user input
if (!empty($_GET["surname"]) &&
    !empty($_GET["firstname"]) &&
    !empty($_GET["phone"]))
{
  if (!($connection =  mysql_connect("localhost", "thatguy", "yahright?")))
     die("Could not connect to database");

  $surname = mysqlclean($_GET, "surname", 50, $connection);
  $firstname = mysqlclean($_GET, "firstname", 50, $connection);
  $phone = mysqlclean($_GET, "phone", 20, $connection);

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

  // Insert the new phonebook entry
  $query = "INSERT INTO phonebook VALUES
            (NULL, '{$surname}', '{$firstname}', '{$phone}')";

  if (mysql_query ($query, $connection))
  {
    header("Location: example.8-5.php?status=T&" .
           "phonebook_id=". mysql_insert_id($connection));
    exit;
  } else {
        // the query failed, do some basic error reporting -
       echo mysql_error();
  }
} // if empty()

header("Location: example.8-5.php?status=F");
?>

its quite clear that you would only be getting that specific error message if case F is executed.  for case F to be executed, this condition is not met:

 

<?php
if (!empty($_GET["surname"]) &&
    !empty($_GET["firstname"]) &&
    !empty($_GET["phone"]))
{
?>

 

make sure you are setting these variables in the URL:

 

http://localhost/example.8-4.php?surname=woods&firstname=tiger&phone=5551234

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.