Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 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
  #1 (permalink)  
Old December 20th, 2004, 05:27 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default need sum help!

howdy,

im totally new to php and need sum help! I tried to get this script working but i keep getting messages with 'query is empty' ..any advice about wot i do wrong? thnx!

Succesfully created a table 'phone_book' in database 'test' in mysql.

create table phone_book(
id integer not null auto_increment,
first_name varchar(50),
last_name varchar(50),
phone varchar(50),
primary key (id)
)

and here's the script that goes with it:


<?php

$table = "phone_book";
$field1 = "first_name";
$field1_label = "First Name";

$field2 = "last_name";
$field2_label = "Last Name";

$field3 = "phone";
$field3_label = "Phone";

$db="test";



mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db);
?>

<html>
<head>
<script language="JavaScript">
function focusform()
{
    document.forms[0].field1_value.focus();
}
</script>
<head>
<body OnLoad="focusform()">

<?PHP

  if (!empty($insert)) {
    mysql_query( "insert into $table ($field1, $field2, $field3) values (\"$field1_value\",\"$field2_value\",\"$field3_val ue\")");
  }
  if (!empty($update)) {
    mysql_query( "update $table set $field1=\"$field1_value\", $field2=\"$field2_value\",$field3=\"$field3_value\ " where id=$update");
  }
  if (!empty($delete)) {
    mysql_query( "delete from $table where id=$delete");
  }
  if (mysql_errno()!=0) {
    switch (mysql_errno()) {
      default:
        echo "Error #".mysql_errno(). " (".mysql_error(). ")<br>";
    }
  }
  if (!$sort) $sort="$field1";
  $query = "select * from $table";
  switch ($sort) {
    case "id": $query=$query. " order by id"; break;
    case "$field1": $query=$query. " order by $field1"; break;
    case "$field2": $query=$query. " order by $field2"; break;
    case "$field3": $query=$query. " order by $field3"; break;
  }
  $result = mysql_query($query);
  $rows = mysql_num_rows($result);
  $i=0;
  echo "<table border=1>\n";
  echo "<tr><td><a href=\"?sort=id\">ID</a></td>\n";
  echo "<td><a href=\"?sort=$field1\">$field1_label</a></td>\n";
  echo "<td><a href=\"?sort=$field2\">$field2_label</a></td>\n";
  echo "<td><a href=\"?sort=$field3\">$field3_label</a></td>\n";
  echo "<td>&nbsp;</td><td>&nbsp;</td></tr>\n";

  echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
  echo "<td>New</td>\n";
  echo "<input type=hidden name=insert value=1></td>\n";
  echo "<td><input type=text size=10 name=field1_value value=$today></td>\n";
  echo "<td><input type=text size=10 name=field2_value></td>\n";
  echo "<td><input type=text size=15 name=field3_value></td>\n";
  echo "<td colspan=2 align=center>";
  echo "<input type=submit value=\" Add \"></td>\n";
  echo "<td></td>\n";
  echo "</form>\n";

  while ($i<$rows) {
    echo "<tr>\n";
    $id=mysql_result($result,$i, "id");
    if (empty($read) || ($read!=$id)) {
      echo "<td>".$id. "</a></td>\n";
      echo "<td>".mysql_result($result,$i, "$field1"). "</td>\n";
      echo "<td>".mysql_result($result,$i, "$field2"). "</td>\n";
      echo "<td>".mysql_result($result,$i, "$field3"). "</td>\n";
      echo "<td><a href=\"?read=$id\">edit</a></td>\n";
      echo "<td><a href=\"?delete=$id\">delete</a></td>\n";
    } else {
      echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
      echo "<td>".$id. "</a></td>\n";
      echo "<input type=hidden name=update value=\"$id\"></td>\n";
      echo "<td><input type=text size=10 name=field1_value value=\"".mysql_result($result,$i, "$field1"). "\"></td>\n";
      echo "<td><input type=text size=10 name=field2_value value=\"".mysql_result($result,$i, "$field2"). "\"></td>\n";
      echo "<td><input type=text size=15 name=field3_value value=\"".mysql_result($result,$i, "$field3"). "\"></td>\n";
      echo "<td colspan=2 align=center><input type=submit value=\" Do it! \"></td>\n";
      echo "</form>\n";
    }
    echo "</tr>\n";
    $i=$i+1;
  }
  mysql_close();

  echo "</table>\n";
?>
</body></html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sum satish_baleri XSLT 6 October 24th, 2007 03:43 AM
how to sum in for-each Gjella XSLT 1 October 2nd, 2006 08:48 AM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
sum utarian Access 2 March 28th, 2005 09:42 PM
SUM or What bertlf Pro VB Databases 2 November 29th, 2003 02:44 PM





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