Wrox Programmer Forums
|
BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0
This is the forum to discuss the Wrox book Beginning PHP5, Apache, and MySQL Web Development by Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass; ISBN: 9780764579660
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 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
 
Old March 18th, 2005, 11:30 PM
Registered User
 
Join Date: Mar 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default I cannot get this code to work , from the book/dl

<?php
require('config.php');

foreach ($_POST as $key => $value) {
  $$key = $value;
}

$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
  or die('Could not connect to MySQL database. ' . mysql_error());
mysql_select_db(SQL_DB, $conn);

switch ($action) {
  case "Create Character":
    $sql = "INSERT IGNORE INTO char_zipcode (id, city, state) " .
           "VALUES ('$zip', '$city', '$state')";
    $result = mysql_query($sql)
      or die(mysql_error());

    $sql = "INSERT INTO char_lair (id, zip_id, lair_addr) " .
           "VALUES (NULL, '$zip', '$address')";
    $result = mysql_query($sql)
      or die(mysql_error());
    if ($result) {
      $lairid = mysql_insert_id($conn);
    }

    $sql = "INSERT INTO char_main (id,lair_id,alias,real_name,align) " .
           "VALUES (NULL, '$lairid', '$alias', '$name', '$align')";
    $result = mysql_query($sql)
      or die(mysql_error());
    if ($result) {
      $charid = mysql_insert_id($conn);
    }

    if ($powers != "") {
      $val = "";
      foreach ($powers as $key => $id) {
        $val[] = "('$charid', '$id')";
      }
      $values = implode(',', $val);
      $sql = "INSERT IGNORE INTO char_power_link (char_id, power_id) " .
             "VALUES $values";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    if ($enemies != '') {
      $val = "";
      foreach ($enemies as $key => $id) {
        $val[] = "('$charid', '$id')";
      }
      $values = implode(',', $val);
      if ($align = 'good') {
        $cols = '(good_id, bad_id)';
      } else {
        $cols = '(bad_id, good_id)';
      }
      $sql = "INSERT IGNORE INTO char_good_bad_link $cols " .
             "VALUES $values";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    $redirect = 'charlist.php';
    break;

  case "Delete Character":
    $sql = "DELETE FROM char_main, char_lair " .
           "USING char_main m, char_lair l " .
           "WHERE m.lair_id = l.id AND m.id = $cid";
    $result = mysql_query($sql)
      or die(mysql_error());

    $sql = "DELETE FROM char_power_link WHERE char_id = $cid";
    $result = mysql_query($sql)
      or die(mysql_error());

    $sql = "DELETE FROM char_good_bad_link " .
           "WHERE good_id = $cid OR bad_id = $cid";
    $result = mysql_query($sql)
      or die(mysql_error());

    $redirect = 'charlist.php';
    break;

  case "Update Character":
    $sql = "INSERT IGNORE INTO char_zipcode (id, city, state) " .
           "VALUES ('$zip', '$city', '$state')";
    $result = mysql_query($sql)
      or die(mysql_error());

    $sql = "UPDATE char_lair l, char_main m " .
           "SET l.zip_id='$zip', l.lair_addr='$address', " .
           "alias='$alias', real_name='$name', align='$align' " .
           "WHERE m.id = $cid AND m.lair_id = l.id";
    $result = mysql_query($sql)
      or die(mysql_error());

    $sql = "DELETE FROM char_power_link WHERE char_id = $cid";
    $result = mysql_query($sql)
      or die(mysql_error());

    if ($powers != "") {
      $val = "";
      foreach ($powers as $key => $id) {
        $val[] = "('$cid', '$id')";
      }
      $values = implode(',', $val);
      $sql = "INSERT IGNORE INTO char_power_link (char_id, power_id) " .
             "VALUES $values";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    $sql = "DELETE FROM char_good_bad_link " .
           "WHERE good_id = $cid OR bad_id = $cid";
    $result = mysql_query($sql)
      or die(mysql_error());

    if ($enemies != '') {
      $val = "";
      foreach ($enemies as $key => $id) {
        $val[] = "('$cid', '$id')";
      }
      $values = implode(',', $val);
      if ($align == 'good') {
        $cols = '(good_id, bad_id)';
      } else {
        $cols = '(bad_id, good_id)';
      }
      $sql = "INSERT IGNORE INTO char_good_bad_link $cols " .
             "VALUES $values";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    $redirect = 'charlist.php';
    break;

  case "Delete Powers":
    if ($powers != "") {
      $powerlist = implode(',', $powers);

      $sql = "DELETE FROM char_power WHERE id IN ($powerlist)";
      $result = mysql_query($sql)
        or die(mysql_error());

      $sql = "DELETE FROM char_power_link " .
             "WHERE power_id IN ($powerlist)";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    $redirect = 'poweredit.php';
    break;

  case "Add Power":
    if ($newpower != '') {
      $sql = "INSERT IGNORE INTO char_power (id, power) " .
             "VALUES (NULL, '$newpower')";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    $redirect = 'poweredit.php';
    break;

  default:

    $redirect = 'charlist.php';
}
header("Location: $redirect");
?>
===========================

I keep getting the old there is an error with your mysql syntax error around " l, char_main m " .
           "SET l.zip_id='$zip'"
I'm new, please help

 
Old April 20th, 2005, 12:33 AM
Registered User
 
Join Date: Apr 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One thing you'll want to make sure of, that you have MySQL 4.0.4 or later. The manual I'm looking at online says that's when multi-table UPDATE was implemented.

I'm not entirely sure, but you might have a problem with the multi-table UPDATE not knowing which table 'alias', 'real_name' or 'align' are supposed to be in. (ie, 1.alias, or m.alias) Everything else in the line (repeated below) is defined.

$sql = "UPDATE char_lair l, char_main m " .
           "SET l.zip_id='$zip', l.lair_addr='$address', " .
           "alias='$alias', real_name='$name', align='$align' " .
           "WHERE m.id = $cid AND m.lair_id = l.id";

I don't see anything else that looks wrong about it, and I haven't had a chance to try the programs yet... I've only had the book for a day.

                                             ..KARL...

<SPAN ATTRIB="DORK">KarlGG</SPAN>





Similar Threads
Thread Thread Starter Forum Replies Last Post
mail through cdosys.dl Amit singla ASP.NET 1.0 and 1.1 Professional 3 October 26th, 2006 06:41 AM
code clinic - Why wont example asp code work? jardbf Classic ASP Basics 3 April 27th, 2006 06:22 PM
How do i dl files that i have uploaded Beginner123 VB How-To 0 March 2nd, 2005 10:42 PM
Code from Book did not work! mnongkhlaw Wrox Book Feedback 2 October 28th, 2003 12:34 AM
Code from Book did not work! mnongkhlaw Classic ASP Databases 0 October 27th, 2003 07:30 AM





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