View Full Version : Function addForm going awry

01-04-2007, 12:37 PM

This is what my code is supposed to do :

a.)Select a customer (say. CompanyD) from db table A. This gives us a variable $cid

b.)Select an address (say. LocationX) from table B (which has a field called $cid corresp. to companies in Table A). This will result in variable $caid

c.)Add the name of a person who works in the LocationX office of CompanyD. This will generate a new record, which will be identified by a field CCID (variable $ccid)

d.)Both customer and address can change for each record. This data will go into table C

Table C also has fields CID, CAID and resp. values of ($cid and $caid)

Now, I have got the foll. code but the addForm function and the code thereafter is plain wrong and i dont know how to set it right . Would someone be able to have a look ? Thanks ! Or if someone knows a more efficient way to code, i dont mind re-doing the whole thing.
I have just put in 1 variable -mobile to test it out and will add the others like name etc once i get this working.

Thanks ! Swati

include ("../include/session.php");


echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>";


$userid = $_SESSION['userid'];


function citySelect ($current, $dis='') {
$sql = "SELECT CITYID, City
FROM Cities
$res = mysql_query($sql) or die(mysql_error());
$str = "<SELECT name='cityid' $dis>\n";
while (list($id,$name)=mysql_fetch_row($res)) {
$selected = $id==$current ? 'selected' : '';
$str .= "<option value=$id $selected>$name</option>\n";
$str .= "</select>\n";
return $str;

function companyList($co) {
$sql = "SELECT CID, Company
FROM Customers
WHERE Company LIKE '%$co%'
ORDER BY Company";
$res = mysql_query($sql) or die(mysql_error());
$str = "<table cellspacing='0' cellpadding='2' border='0'>\n";
while (list($id,$name)=mysql_fetch_row($res)) {
$str .= "<tr><td><a href='?action=select&amp;company=$id'>Select</td><td>$name</td></tr>\n";
$str .= "</table>";
return $str;

function addressList($cid) {
global $fmCompany;
$sql = "SELECT a.CAID, a.Street, a.CITYID, a.Pincode, a.Areacode, a.Phone, a.Fax, a.Email, c.Company
FROM Custaddress a
INNER JOIN Customers c ON a.CID = c.CID
WHERE a.CID = '$cid'";
$res = mysql_query($sql) or die(mysql_error());
$str = '<form method="post">
<input type="hidden" name="fname" value="fmAddress">
<table border="1"><center><font face=Verdana size=2 color=white>
while (list($caid, $st, $city, $pc, $ac, $ph, $fax, $em, $co)=mysql_fetch_row($res)) {
$fmCompany = $co;
$str .= "<tr><td>$st</td><td>" . citySelect($city, 'disabled') . "</td><td>$pc</td><td>$ac</td><td>$ph</td><td>$fax</td><td>$em</td>
<td><input type='radio' name='address' value='$caid'></td></tr>";
$str .= '</table><br>
<input type="submit" name="action" value="Select address where the new person can be contacted">
return $str;

function addForm() {
global $fmCompany;
$str = "<form method='post'>
<input type='hidden' name='caid' value='$caid'>
<input type='hidden' name='cid' value='$cid'>
<table border='1'><center><font face=Verdana size=2 color=white>";

$fmCompany = $co;
$str .= "<tr><td>Mobile</td><td><input type='text' value='$mobile' size='100'></td></tr>

<tr><td>&nbsp</td><td><input type='submit' name='action' value='INSERT'></td></tr>";

$str .= '</table></form>';
return $str;

$sql = "INSERT INTO Custcontact (CID, CAID ,Mobile) VALUES ('$cid', '$caid', '$mobile') WHERE CAID = '{$_POST['caid']}' AND CID = '{$_POST['cid']}' ";
echo "<center><font face=Verdana size=2 color=white>Contact Added";

if (isset($_REQUEST['action'])) {

switch ($_REQUEST['action']) {
case 'Search':
$srch = $_POST['srch'];
$fmCompany = stripslashes($_POST['srch']);
$body = companyList($srch);
case 'select':
$body = addressList($_GET['company']);
case 'Select address where the new person can be contacted':
$body = addForm($_POST['address']);
case 'Insert':
$body = '';
else $body = '';
<form name='fmSearch' method='POST'>
<input type="hidden" name="fname" value="fmSearch"><center><font face='Verdana' size='2' color=white>
Company Name <input type="text" name="srch" value="<?php echo $fmCompany ?>" size="80">
<input type="submit" name="action" value="Search">

<?php echo $body ?>

01-05-2007, 12:31 AM
First of all, I would look at the design of your database, your tables should be normalised.
Consider if the customer and address details really need to be separate etc.
If you provide a proper description of what you are trying to achieve rather than 'what if scenario', I might be able to guide you through the design of both the db and subsequent functions.
If you are not comfortable posting stuff here, you can message me via this forum.

Cheers :thumbsup:

01-05-2007, 03:08 AM
The database is ok . I need to have the data in separate tables because of many to one and one to many relationships.

Also, i have contacts who work out of 2 office locations of a company so i cannot be typing out the same address multiple times.

The problem arises with the way i have defined addForm and the subsequent insert and isset operations. There's a problem them that i'm not able to straighten out !

Any ideas ? Thanks