Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases 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 July 27th, 2006, 04:25 PM
Registered User
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to haryohnesa Send a message via MSN to haryohnesa Send a message via Yahoo to haryohnesa
Default someonehelp me on this code..

i'm trying to use the example given in chapter 10 of PHP5, Apache, MySQL... i managed removing the enemies area but i find it dificult to implement the create character to work with the changes i made to the codes itself i know it would take me a long time/hours to figure it out but it's killing my deadline to my personal PHP skills development... i enclosed the codes you might want to look into..

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

if (!isset($_GET['c']) || $_GET['c'] == '' || !is_numeric($_GET['c'])) {
  $char='0';
} else {
  $char = $_GET['c'];
}
$subtype = "Create";
$subhead = "Please enter character data and click " .
           "'$subtype Character.'";
$tablebg = '#EEEEFF';

$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);

$sql = "SELECT id, cat_name FROM categories";
$result = mysql_query($sql)
  or die(mysql_error());
if (mysql_num_rows($result) > 0) {
  while ($row = mysql_fetch_array($result)) {
    $categorylist[$row['id']] = $row['cat_name'];
  }
}

$sql = "SELECT id, fname FROM member_main WHERE id != $char";
$result = mysql_query($sql)
  or die(mysql_error());
if (mysql_num_rows($result) > 0) {
  $row = mysql_fetch_array($result);
  $memberlist[$row['id']] = $row['fname'];
}

if ($char != '0') {
  $sql = "SELECT c.fname, c.lname AS name, c.gender, " .
         "l.addr AS address, l.addr2 AS address2, l.province, z.city, z.state, z.id AS zip " .
         "FROM member_main c, member_add l, member_zipcode z " .
         "WHERE z.id = l.zip_id " .
         "AND c.lair_id = l.id " .
         "AND c.id = $char";
  $result = mysql_query($sql)
    or die(mysql_error());
  $ch = mysql_fetch_array($result);

  if (is_array($ch)) {
    $subtype = "Update";
    $tablebg = '#EEFFEE';
    $subhead = "Edit data for <i>" . $ch['fname'] .
               "</i> and click '$subtype Character.'";

    $sql = "SELECT p.id " .
           "FROM member_main c " .
           "JOIN categories p " .
           "JOIN userlookup pk " .
           "ON c.id = pk.member_id " .
           "AND p.id = pk.cat_id " .
           "WHERE c.id = $char";
    $result = mysql_query($sql)
      or die(mysql_error());
    if (mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_array($result)) {
        $cats[$row['id']] = 'selected';
      }
    }

    }
  }
?>

<html>
<head>
<title>Character Editor</title>
</head>
<body>
<img src="CBA_Tiny.gif" align="left" hspace="10">
<h1>Comic Book<br />Appreciation</h1><br />
<h3><?php echo $subhead; ?></h3>

<form action="process_transact.php" name="theform" method="post">
<table border="0" cellpadding="1" bgcolor="<?php echo $tablebg; ?>">
  <tr>
    <td>FirstName:</td>
    <td><input type="text" name="alias" size="41"
          value="<?php if (isset($ch)) { echo $ch['fname']; } ?>">
    </td>
  </tr>
  <tr>
    <td>LastName:</td>
    <td><input type="text" name="name" size="41"
          value="<?php if (isset($ch)) { echo $ch['name']; } ?>">
    </td></tr>
    <tr>
    <td>Province</td>
    <td>
            <input type="text" name="province"
          value="<?php if (isset($ch)) { echo $ch['province']; } ?>">
    </td>
  </tr>
  <tr>
    <td>Powers:<br>
      (Ctrl-click to<br>select multiple<br>powers)
    </td>
    <td>
      <select name="cats[]">
      <option value="">Choose Category</option>
        <option value="">____________</option>


<?php
foreach ($categorylist as $key => $value) {
  echo "<option value=\"$key\" ";
  if (isset($cats) && array_key_exists($key,$cats)) {
    echo $cats[$key];
  }
  echo ">$value</option>\n";
}
?>

      </select>
    </td>
  </tr>

  <tr>
    <td>Lair Location:<br>
      (address1,address2,<br>city, state, zip)
    </td>
    <td><input type="text" name="address" size="41"
          value="<?php if (isset($ch)) { echo $ch['address']; } ?>"><br>
        <input type="text" name="address" size="41"
          value="<?php if (isset($ch)) { echo $ch['address2']; } ?>"><br>
        <input type="text" name="city"
          value="<?php if (isset($ch)) { echo $ch['city']; } ?>">
        <input type="text" name="state" size="2"
          value="<?php if (isset($ch)) { echo $ch['state']; } ?>">
        <input type="text" name="zip" size="10"
          value="<?php if (isset($ch)) { echo $ch['zip']; } ?>">
    </td>
  </tr>

  <tr>
    <td>Alignment:</td>

    <td>
      <input type="radio" name="align" value="good"
      <?php if (isset($ch)) {
        echo($ch['gender']=='male' ? ' checked' : '');
      } ?>>
      good<br>
      <input type="radio" name="align" value="evil"
      <?php if (isset($ch)) {
        echo($ch['gender']=='female' ? ' checked' : '');
      } ?>>
      evil
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <input type="submit" name="action"
        value="<?php echo $subtype; ?> Character">
      <input type="reset">
<?php if ($subtype == "Update") { ?>

      <input type="submit" name="action" value="Delete Character">
<?php } ?>
    </td>
  </tr>
</table>
<input type="hidden" name="cid" value="<?php echo $char; ?>">
</form>
<a href="memberlist.php">Return to Home Page</a>
</body>
</html>


char_transect.php

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

foreach ($_POST as $key => $value) {
  $$key = $value;
}
// login session would be here to access client info.

$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 member_zipcode (id, city, state, country) " .
           "VALUES ('$zip', '$city', '$state', '$country')";
    $result = mysql_query($sql)
      or die(mysql_error());

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

    $sql = "INSERT INTO member_main (id, lair_id, fname, lname, username, password, email,
                                    dateofbirth, regdate, phone, gender) " .
           "VALUES (NULL, '$lairid', '$fname', '$lname', '$username', PASSWORD('$password'),
                       '$email', '$dateofbirth', 'NOW()', '$phone', '$gender')";

    $result = mysql_query($sql)
      or die(mysql_error());
    if ($result) {
      $memberid = mysql_insert_id($conn);
    }

    if ($categories != "") {
      $val = "";
      foreach ($categories as $key => $id) {
        $val[] = "('$memberid', '$id')";
      }
      $values = implode(',', $val);
      $sql = "INSERT IGNORE INTO userlookup (member_id, cat_id) " .
             "VALUES $values";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    $redirect = 'memberlist.php';
    break;

// delete everything about a member when its function is no longer needed...

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

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

    $redirect = 'memberlist.php';
    break;

// update member information and every information that pertains to his table...

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

    $sql = "UPDATE member_add l, member_main m " .
           "SET l.zip_id='$zip', l.addr1='$address', l.addr2='$address2', l.province='$province'" .
           "fname='$fname', lname='$lname', username='$username', password=PASSWORD('$password'), email='$email', dateofbirth='$dateofbirth', phone='$phone', update='NOW()' " .
           "WHERE m.id = $cid AND m.lair_id = l.id";
    $result = mysql_query($sql)
      or die(mysql_error());

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

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


    $redirect = 'memberlist.php';
    break;

  case "Delete Category":
    if ($category != "") {
      $categorylist = implode(',', $category);

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

      $sql = "DELETE FROM userlookup " .
             "WHERE cat_id IN ($categorylist)";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    $redirect = 'categoryedit.php';
    break;

  case "Add Categories":
    if ($newcat != '') {
      $sql = "INSERT IGNORE INTO categories (id, cat_name) " .
             "VALUES (NULL, '$newcat')";
      $result = mysql_query($sql)
        or die(mysql_error());
    }

    $redirect = 'categoryedit.php';
    break;

  default:

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


haryoh





Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent:hard disk serial code and vb code ivanlaw Pro VB 6 0 July 25th, 2007 04:05 AM
VB: .Exe file, serial code and activation code ivanlaw Pro VB 6 8 July 6th, 2007 05:44 AM
code clinic - Why wont example asp code work? jardbf Classic ASP Basics 3 April 27th, 2006 06:22 PM
Writing Client Side Script from Code-Behind code sajid_pk Classic ASP Databases 1 January 18th, 2005 12:53 AM





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