...

View Full Version : trouble with lookup table



bateman
10-26-2006, 08:28 PM
I am having trouble getting data to enter my lookup table, but no problems with data entering my other tables. Could someone give me any suggestions? Thanks in advance.

Heres the code:


<?php
$link = mysql_connect('localhost', 'root', '');

if (!mysql_select_db('lbd')) {
exit('<p>Unable to locate database.</p>');
}
?>

<?php

$businesses = mysql_query('SELECT id, name FROM business');
if (!$businesses) {
exit('<p>Unable to obtain business list.</p>');
}

$categories = mysql_query('SELECT id, type FROM category');
if (!$categories) {
exit('<p>Unable to obtain category list.</p>');
}

$regions = mysql_query('SELECT id, name FROM region');
if (!$regions) {
exit('<p>Unable to obtain region list.</p>');
}
?>

<?php
if (!get_magic_quotes_gpc()) {
$_GET = array_map('addslashes', $_GET);
$_POST = array_map('addslashes', $_POST);
$_COOKIE = array_map('addslashes', $_COOKIE);
$_REQUEST = array_map('addslashes', $_REQUEST);
}
?>
</div>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p align="left">Business:
<select name="bid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php
while ($business = mysql_fetch_array($businesses)) {
$bid = htmlspecialchars($business['id']);
$bname = $business['name'];
echo "<option value='$bid'>$bname, $bid</option>\n";
}
?>
</select></p>
<p align="left">Name: <input type="text" name="name" /></p>
<p align="left">Address: <input type="text" name="address" /></p>
<p align="left">City: <input type="text" name="city" /></p>
<p align="left">State: <input type="text" name="state" /></p>
<p align="left">Zip: <input type="text" name="zip" /></p>
<p align="left">Phone: <input type="text" name="phone" /></p>
<p align="left">Region: <input type="text" name="regionid" /></p>
<p align="left">Place in categories:<br />
<?php
while ($cat = mysql_fetch_array($categories)) {
$cid = $cat['id'];
$ctype = htmlspecialchars($cat['type']);
echo "<label><input type='checkbox' name='cats[]' " .
"value='$cid' />$ctype, $cid</label><br />\n";
}
?>
</p>
<div align="left">
<input type="submit" value="SUBMIT" />
</div>
</form>

<div align="left">
<?php

if (isset($_POST['name'])) {
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$rid = $_POST['regionid'];
$lid = mysql_insert_id();
$sql = "INSERT INTO listing SET
id='$lid',
name='$name',
address='$address',
city='$city',
state='$state',
zip='$zip',
phone='$phone',
regionid='$rid',
date=CURDATE(),
businessid='$bid'";

if (mysql_query($sql)) {
echo '<p>New listing added</p>';
} else {
exit('<p>Error adding new listing: ' . mysql_error() . '</p>');
}
}


if (isset($_POST['cid'])) {
$cid = $_POST['cid'];
} else {
$cid = array();
}

$numCats = 0;
foreach ($cid as $catid) {
$sql = "INSERT IGNORE INTO listingcategory SET listingid=$lid, categoryid=$catid";
$ok = mysql_query($sql);
if ($ok) {
$numCats = $numCats + 1;
} else {
echo "<p>Error inserting listing into category $catid: " .
mysql_error() . '</p>';
}
}

?>

</div>

<p align="left">Listing was added to <?php echo $numCats; ?> categories.</p>
<p align="left"><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another listing</a></p>
<p align="left"><a href="listings.php">Return to listing search</a></p>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum