Hi, I am getting an error and the contacts aren't going into the database for some reason. I have 4 tables - regions table, contacts table, org table, contactregion table. The regions table fields are id and name. Same for org. Contactregion fields are contactid and regionid (both integers). Contact fields are id, contact_name, orgid, etc
Here is the page I am having trouble with
<?php
include("../Connections/conn.php");
if (isset($_POST['contact_name'])):
// A new contact has been entered
// using the form.
$oid = $_POST['oid'];
$contact_name = $_POST['contact_name'];
$contact_name2 = $_POST['contact_name2'];
$title = $_POST['title'];
$name = $_POST['name'];
$title_2 = $_POST['title_2'];
$name_2 = $_POST['name_2'];
$res_add = $_POST['res_add'];
$postal_add = $_POST['postal_add'];
$phone = $_POST['phone'];
$mobile = $_POST['mobile'];
$fax = $_POST['fax'];
$email = $_POST['email'];
if ($contact_name == '') {
exit('<p>You must type in a name for this contact. Click "Back" and try again.</p>');
}
if ($oid == '') {
exit('<p>You must choose an organisation for this contact. Click "Back" and try again.</p>');
}
if ($cats == '') {
exit('<p>You must choose a region for this contact. Click "Back" and try again.</p>');
}
$sql = "INSERT INTO contact SET
contact_name='$contact_name',
contact_name2='$contact_name2',
title='$title',
name='$name',
title_2='$title_2',
name_2='$name_2',
res_add='$res_add',
postal_add='$postal_add',
phone='$phone',
mobile='$mobile',
fax='$fax',
email='$email',
orgid='$oid'";
if (@mysql_query($sql)) {
echo '<h4>New contact added</h4>';
} else {
exit('<p>Error adding new contact: ' . mysql_error() . '</p>');
}
$jid = mysql_insert_id();
if (isset($_POST['cats'])) {
$cats = $_POST['cats'];
} else {
$cats = array();
}
$numCats = 0;
foreach ($cats as $catID) {
$sql = "INSERT IGNORE INTO contactregion
SET contactid=$jid, regionid=$catID";
$ok = @mysql_query($sql);
if ($ok) {
$numCats = $numCats + 1;
} else {
echo "<p>Error inserting contact into region $catID: " .
mysql_error() . '</p>';
}
}
?>
<p>Contact was added to <?php echo $numCats; ?> region(s).</p>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another contact</a></p>
<p><a href="pub.php">Return to Manage Contacts</a></p>
<?php
else: // Allow the user to enter a new contact
$cats = @mysql_query('SELECT id, name FROM region ORDER BY name ASC');
if (!$cats) {
exit('<p>Unable to obtain region list from the database.</p>');
}
$orgs = @mysql_query('SELECT id, name FROM org ORDER BY name ASC');
if (!$orgs) {
exit('<p>Unable to obtain organisation list from the database.</p>');
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Enter the new contact:<br />
<input name="contact_name" type="text" value="
" size="100" />
</p>
<p>Enter the second part of contact if there is one:<br />
<input name="contact_name2" type="text" value="
" size="100" />
</p>
<p>Enter contact title:<br /> <input name="title" type="text" value="
" size="50" /></p>
<p>Enter contact name:<br /> <input name="name" type="text" value="
" size="50" /></p>
<p>Enter contact 2 title:<br />
<input name="title_2" type="text" value="
" size="50" />
</p>
<p>Enter contact 2 name:<br />
<input name="name_2" type="text" value="
" size="50" />
</p>
<p>Enter residential address:<br />
<input name="res_add" type="text" value="
" size="255" /></p>
<p>Enter postal address:<br />
<input name="post_add" type="text" value="
" size="255" /></p>
<p>Enter phone number:<br />
<input name="phone" type="text" value="
" size="20" /></p>
<p>Enter fax number:<br />
<input name="fax" type="text" value="
" size="20" /></p>
<p>Enter mobile number:<br />
<input name="mobile" type="text" value="
" size="20" /></p>
<p>Enter email:<br />
<input name="email" type="text" value="
" size="20" /></p>
<p>Region(s)<br />
<?php
while ($cat = mysql_fetch_array($cats)) {
$cid = $cat['id'];
$cname = htmlspecialchars($cat['name']);
echo "<label><input type='checkbox' name='cats[]' value='$cid' />$cname</label><br />\n";
}
?>
</p>
<p>Organisation:
<select name="oid" size="1">
<option selected value="">Select One</option>
<option value="">---------</option>
<?php
while ($org = mysql_fetch_array($orgs)) {
$oid = $org['id'];
$oname = htmlspecialchars($org['name']);
echo "<option value='$oid'>$oname</option>\n";
}
?>
</select>
</p>
<input type="submit" value="SUBMIT" />
</form>
<?php endif; ?>
The error I am getting is:
New Contact Added
Error inserting contact into region 13: unknown column 'regionid' in field list.
Error inserting contact into region 15: unknown column 'regionid' in field list.
Contact was added to 0 regions.
Add another contact.
Return to manage contacts.
Any help would be appreciated.