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
|