Jump to content

Problem with moving textboxes, checkboxes, textarea to MySQL


thief425

Recommended Posts

To be frank and honest up front, I've always been scared of php.  I haven't done html in a couple of years, but I'm trying to put together a rather simple form to collect information from a form and post it to a database for online storage.  The field I work in employs a huge referral network, and I want to create an online database that allows agencies to list themselves for storage and search later.  I've tried to piece together the following pages for data entry and pushing to the database, but when I run it, it updates every row of the database with the information from the array. 

 

I'm sure that I'm doing something totally wrong, so don't bash me for that.  I don't expect anyone to write a ton of code for me, just help me find the breakdown that is causing my db to change the entire column when I submit a new form.  I suspect that my problem lies in the relationships between the following parts of the code, but my inexperience doesn't help me figure out what is actually going wrong.  Thanks in advance for your help.  Also, I can't get textarea on the form to submit anything, ever.  It's just blank.  I've tried it a number of ways, thinking maybe I was missing something minor, so take what you see there with a grain of salt.

 

mysql_query ("INSERT INTO Agency_Contacts (category, agency_name, primary_contact_name, phone_no, email_web, street, city, state, region, client_pop, hours, services) 
VALUES ('$category', '$agency_name', '$primary_contact_name', '$phone_no', '$email_web', '$street', '$city', '$state', '$region', '$client_pop', '$hours', '$services')
"); 
if ($CATEGORY_ARRAY)
{
$CATEGORY = implode($CATEGORY_ARRAY, ",");
$result = mysql_query ("UPDATE Agency_Contacts
SET CATEGORY = '$CATEGORY'
");
if(!$result) 
{
echo "<B>UPDATE unsuccessful:</b> ", mysql_error();
exit;
}
}

 

 

 

 

DataEntry.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="Agency Submission" name="form1" method="GET" action="../../cgi-bin/submitform.php">
    Agency Category (check all that apply):
<br />
  <label>
  <input type="checkbox" name="CATEGORY_ARRAY[]" id="Administrative" value="Administrative" />
  Administrative</label>
  <label>
  <br />
  <input type="checkbox" name="CATEGORY_ARRAY[]" id="Advocacy/Social Justice" value="Advocacy/Social Justice" />
  Advocacy/Social Justice</label>
  
  <label><br />
  <input type="checkbox" name="CATEGORY_ARRAY[]" id="Community Practice" value="Community Practice" />
  Community Practice</label>
  <p>Agency Name: 
    <input type=text name="agency_name" size=75 maxlength=75>
  </p>
  <p>Primary Contact: 
    <input type=text name="primary_contact_name" size=25 maxlength=25>
  </p>
  <p>
    Contact Phone no.: 
    <input name=phone_no type=text value="(555)555-1212" size=25 maxlength=25>
    </p>
  <p>Email/Website: 
    <input name=email_web type=text value="Separate with a comma" size=25 maxlength=75>
    </p>
  <p>Street Address: 
    <input type=text name=street size=25 maxlength=50>
    </p>
  <p>City: 
    <input type=text name=city size=25 maxlength=25>
    </p>
  <p>State: 
    <input name=state type=text value="XX" size=6 maxlength=2>
    </p>
  Region of your state in which services are available (check all that apply):  
  <label>
  <br />
  <input type="checkbox" name="REGION_ARRAY[]" id="North" value="North" />
  North</label>
  <label>
  <br />
  <input type="checkbox" name="REGION_ARRAY[]" id="East" value="East" />
  East</label>  
  <label><br />
  <input type="checkbox" name="REGION_ARRAY[]" id="South" value="South" />
  South</label>
  <label><br />
  <input type="checkbox" name="REGION_ARRAY[]" id="West" value="West" />
  West</label>
   
  <p>Client populations served (select all that apply):  </p>
  <label>
  <input type="checkbox" name="CLIENT_POP_ARRAY[]" id="Adult-Male" value="Adult-Male" />
  Adult-Male</label>
  <label>
  <br />
  <input type="checkbox" name="CLIENT_POP_ARRAY[]" id="Adult-Female" value="Adult-Female" />
  Adult-Female</label>  
  <label>
  <br />
  <input type="checkbox" name="CLIENT_POP_ARRAY[]" id="Child/School" value="Child/School" />
  Child/School</label>
  <label><br />
  <input type="checkbox" name="CLIENT_POP_ARRAY[]" id="Domestic Violence" value="Domestic Violence" />
  Domestic Violence</label>
  
  <p>Services Available:  </p>
  <p>
    <textarea cols=75 rows=5 name="SERVICES_ARRAY" value="">List services separated by a comma</textarea>
  </p>
  <p>
<input type=submit>

</form>
<p>  </p>
</body>
</html>

 

submitform.php

<html>
<body>
<p><?php 
$link = mysql_connect('host', 'user', 'pass'); 
mysql_select_db(resources2010); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully';
mysql_query ("INSERT INTO Agency_Contacts (category, agency_name, primary_contact_name, phone_no, email_web, street, city, state, region, client_pop, hours, services) 
VALUES ('$category', '$agency_name', '$primary_contact_name', '$phone_no', '$email_web', '$street', '$city', '$state', '$region', '$client_pop', '$hours', '$services')
"); 
if ($CATEGORY_ARRAY)
{
$CATEGORY = implode($CATEGORY_ARRAY, ",");
$result = mysql_query ("UPDATE Agency_Contacts
SET CATEGORY = '$CATEGORY'
");
if(!$result) 
{
echo "<B>UPDATE unsuccessful:</b> ", mysql_error();
exit;
}
}
if ($REGION_ARRAY)
{
$REGION = implode($REGION_ARRAY, ",");
$result = mysql_query ("UPDATE Agency_Contacts
SET REGION = '$REGION'
");
if(!$result) 
{
echo "<B>UPDATE unsuccessful:</b> ", mysql_error();
exit;
}
}
if ($CLIENT_POP_ARRAY)
{
$CLIENT_POP = implode($CLIENT_POP_ARRAY, ",");
$result = mysql_query ("UPDATE Agency_Contacts
SET CLIENT_POP = '$CLIENT_POP'
");
if(!$result) 
{
echo "<B>UPDATE unsuccessful:</b> ", mysql_error();
exit;
}
}
if ($SERVICES_ARRAY)
{
$SERVICES = implode($SERVICES_ARRAY, ",");
$result = mysql_query ("UPDATE Agency_Contacts
SET SERVICE = '$SERVICES'
");
if(!$result) 
{
echo "<B>UPDATE unsuccessful:</b> ", mysql_error();
exit;
}
}

print ("Thanks for submitting your agency information.");
?>
</p>
</body>
</html>

 

Link to comment
Share on other sites

Also, I wanted to add that I have been able to get the form to write to the database without writing to the entire column, but it creates a new record for that single field, and doesn't add to the record where the textboxes are writing to.

Link to comment
Share on other sites

Looks all good except for the

VALUES ('$category', '$agency_name', '$primary_contact_name', '$phone_no', '$email_web', '$street', '$city', '$state', '$region', '$client_pop', '$hours', '$services')

I may be off my rocker but isn't it supposed to be

'$_POST[category]','$_POST[agency_name]', etc etc etc

?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.