Please help with DB:Syntax error

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
bk6662
Forum Newbie
Posts: 2
Joined: Wed Sep 08, 2010 9:58 pm

Please help with DB:Syntax error

Post by bk6662 »

Hello,

I am relatively new to PHP, but not to programming. I've recently installed PHP5, MySQL and Apache on a Linux machine. Everything is working great. I came across the book "Beginning PHP4 Databases", put out by Wrox Press. I'm working on a sample application called "Accounts Receivable". The interface works, and I can enter invoices into the database with one of the modules, so that appears to be set up properly. My problem is with most of the modules that pull information from the DB.

I will include one of the shorter modules (Invoice Review Detail) which contains this problem. I think I've pinpointed it to the 3rd "Die" statement in the code. If I comment out that particular line I no longer get the syntax error. Although I then get a "Fatal error: Call to undefined method DB_Error::fetchRow() in /var/www/Accounting/ir.php on line 46" - which is the following line. I'm sure that's a different problem, possibly related to the first. This is in a (fetchRow) statement. If I also comment out that line, the page comes up (but of course with no functionality).

Here's the code. Please let me know what I'm missing; everything looks right to my untrained eyes. (Although I've already found quite a few errors in this sample code; I'm sure there's a logic error that I'm missing):

Code: Select all

<?php
require_once("DB.php");
require_once("billing_includes.php");

$db = DB::connect($dsn);
if( DB::isError($db) ) {
  die($db->getMessage());
}

if( isset($_GET["invoice_id"]) )  {
  $invoice_id = $_GET["invoice_id"];
}
if( isset($_POST["invoice_id"]) ) {
  $invoice_id = $_POST["invoice_id"];
}

if( 1 == $_POST["updatepaid"] ) {
  $sql = "UPDATE invoices
    SET invoice_paid = 1
    WHERE invoice_id = " . $invoice_id;

  $rs = $db->query($sql);
  if( DB::isError($rs) ) {
    die ($rs->getMessage());
  }
}


// Set up SQL and perform query
$sql = "SELECT i.invoice_id, description_short, description_full,
              date_format(date_posted, '%Y-%m-%d') AS date_posted,
              date_format(date_due, '%Y-%m-%d') AS date_due
              amount_due, invoice_paid, client_firstname,
              client_lastname, client_street, client_city,
              client_state, client_zip, client_phone
         FROM invoices i, clients c, client_invoices ci
         WHERE i.invoice_id = $invoice_id
         AND ci.invoice_id = $invoice_id
         AND c.client_id = ci.client_id";

$rs = $db->query($sql);
if( DB::isError($rs) )  {
[b]//  die ($rs->getMessage());[/b]}

[b]$invoice = ($rs->fetchRow (DB_FETCHMODE_ASSOC));[/b]

?>

<html>
  <head>
    <title=Billing System</title>
    <style type="text/css" media="screen">
      @import url(billing.css);
    </style>
  </head>
  <body>

  <div id="pageheader">
    <a href="index.php">Billing System</a> :: Review Invoice
  </div><!-- end div pageheader -->

  <div id="pagecontent">

  <div class="sectionheading">
    Invoice ID: <?php echo $invoice["invoice_id"]; ?>
  </div>

  <table border="0">
  <tr>
    <td valign="top" class="darker" width="200">Client:</td>
    <td class="darker" width="300">
  <?php
  echo $invoice["client_firstname"] . " " . $invoice["client_lastname"] .
    "<br />" . $invoice["client_street"] . "<br />" . $invoice["client_city"] .
    ", " . $invoice["client_state"] . " " . $invoice["client_zip"] . "<br />" .
    $invoice["client_phone"];
  ?>
    </td>
  </tr>
  <tr>
    <td valign="top" class="lighter" width="200">Short Description:</td>
    <td class="lighter" width="300"><?php echo
       smart_strip_slashes($invoice["description_short"]); ?></td>
  </tr>
  <tr>
    <td valign="top" class="darker" width="200">Full Description:</td>
    <td class="darker" width="300"><?php echo
       smart_strip_slashes($invoice["description_full"]); ?></td>
    </td>
  </tr>
  <tr>
    <td valign="top" class="lighter" width="200">Amount Due:</td>
    <td class="lighter" width="300">$<?php echo
       number_format($invoice["amount_due"], 2); ?></td>
  </tr>
  <tr>
    <td valign="top" class="darker" width="200">Date Posted:</td>
    <td class="darker" width="300"><?php echo
       reformat_date($invoice["date_posted"]); ?></td>
  </tr>
  <tr>
    <td valign="top" class="lighter" width="200">Date Due:</td>
    <td class="lighter" width="300"><?php echo
       reformat_date($invoice["date_due"]); ?><\td>
  </tr>
  <tr>
    <td colspan="2" align="left">
  <?php
  if( 0 == $invoice["invoice_paid"] ) {
  ?>

    <form name="updatepaid" action="<?php echo $_SERVER["PHP_SELF"];
      ?>" method="post">
      <input type="hidden" name="updatepaid" value="1">
      <input type="hidden" name="invoice_id" value="<?php echo
        $invoice["invoice_id"]; ?>">
      <input type="submit" value="Set Invoice as Paid">
    </form>

  <?php  }  else  {?>
//    echo "This Invoice has been paid."
  <?php  }  ?>
    </td>
  </tr>
  </table>

  </div>
</html>
Thanks in advance!
Brian
bk6662@cox.net
Last edited by Benjamin on Thu Sep 09, 2010 12:44 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Please help with DB:Syntax error

Post by amargharat »

Check whether fetchrow function exsits in DB.php
bk6662
Forum Newbie
Posts: 2
Joined: Wed Sep 08, 2010 9:58 pm

Re: Please help with DB:Syntax error

Post by bk6662 »

Hi Amargharat,

I appreciate your suggestion - Yes the fetchRow function does indeed exist within DB.php. That would definitely explain the second error I reported. But unless I'm wrong I don't think it would have anything to do with the line "die ($rs->getMessage());" causing a DB:Syntax error? It's consistent - as soon as I comment" out that one line, I can get the code to run. (except for the other error pointing to the fetchRow function).

Please let me know if you have anything else I can check - this is really bugging me. It's strange I can use the exact same syntax in another area without a problem, but here it's not working. Can it be a scope issue? But DB.php is included globally, so I don't know how that could possibly be the case.

Thanks again!
Brian
Post Reply