Forum Moderators: coopster

Message Too Old, No Replies

checkboxes on inserting values in MySQL database table

trouble getting checkbox data to show up in MySQL database

         

frella

5:40 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



I am very new at php and have created a form that has many checkboxes. I have created a table in phpMyAdmin, but the values of the checkboxes are not showing up in my database table. I think I don't have the correct field attributes set for checkboxes, because now I have it set to VARCHAR.

Here is a few of the checkbox html code samples of my form:
<input type="checkbox" name="service[]" value="ExpressSaver">The Express Saver<br />
<input type="checkbox" name="service[]" value="PremiumDetail">The Premium Detail<br />
etc. and it continues with a bunch of other choices.

the php is:
<?
include("dbinfo.inc.php");
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$company = mysql_real_escape_string($_POST['company']);
$phone = mysql_real_escape_string($_POST['phone']);
$email = mysql_real_escape_string($_POST['email']);
$vehicle = mysql_real_escape_string($_POST['vehicle']);
$color = mysql_real_escape_string($_POST['color']);
$ExpressSaver = mysql_real_escape_string($_POST['ExpressSaver']);
$PremiumDetail = mysql_real_escape_string($_POST['PremiumDetail']);
$WorksDetail = mysql_real_escape_string($_POST['WorksDetail']);
$UltimateDetail = mysql_real_escape_string($_POST['UltimateDetail']);
$InteriorShampoo = mysql_real_escape_string($_POST['InteriorShampoo']);
$InteriorDetail = mysql_real_escape_string($_POST['InteriorDetail']);
$FullSize = mysql_real_escape_string($_POST['FullSize']);
$HandWax = mysql_real_escape_string($_POST['HandWax']);
$TarBug = mysql_real_escape_string($_POST['TarBug']);
$Dent = mysql_real_escape_string($_POST['Dent']);
$PaintChip = mysql_real_escape_string($_POST['PaintChip']);
$WindshieldChip = mysql_real_escape_string($_POST['WindshieldChip']);
$Engine = mysql_real_escape_string($_POST['Engine']);
$RimsBuff = mysql_real_escape_string($_POST['RimsBuff']);
$nonautoExteriorWash = mysql_real_escape_string($_POST['nonautoExteriorWash']);
$nonautoInteriorDetail = mysql_real_escape_string($_POST['nonautoInteriorDetail']);
$nonautoInteriorVac = mysql_real_escape_string($_POST['nonautoInteriorVac']);
$nonautoWax = mysql_real_escape_string($_POST['nonautoWax']);
$nonautoOxidation = mysql_real_escape_string($_POST['nonautoOxidation']);
$nonautoAcid = mysql_real_escape_string($_POST['nonautoAcid']);
$nonautoMotorcycleVinyl = mysql_real_escape_string($_POST['nonautoMotorcycleVinyl']);
$nonautoMotorcycleChrome = mysql_real_escape_string($_POST['nonautoMotorcycleChrome']);
$nonautoMotorcycleBug = mysql_real_escape_string($_POST['nonautoMotorcycleBug']);
$nonautoRims = mysql_real_escape_string($_POST['nonautoRims']); $query =
"INSERT INTO Detailing VALUES ('','$firstname','$lastname','$company','$phone','$email','$vehicle','$color', '$ExpressSaver', '$PremiumDetail', '$WorksDetail', '$UltimateDetail', '$InteriorShampoo', '$InteriorDetail', '$FullSize', '$HandWax', '$TarBug', '$Dent', '$PaintChip', '$WindshieldChip', '$Engine', '$RimsBuff', '$nonautoExteriorWash', '$nonautoInteriorDetail', '$nonautoInteriorVac', '$nonautoWax', '$nonautoOxidation', '$nonautoAcid', '$nonautoMotorcycleVinyl', '$nonautoMotorcycleChrome', '$nonautoMotorcycleBug', '$nonautoRims')";
mysql_query($query);
mysql_close();


Any ideas of what is going wrong here?

rocknbil

6:02 pm on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are setting the checkbox values as an array named service

service[]

and expecting to extract them by their value.

$ExpressSaver = mysql_real_escape_string($_POST['ExpressSaver']);
$PremiumDetail = mysql_real_escape_string($_POST['PremiumDetail']);

You would (most likely) look for $_POST[service[0]] and $_POST[service[1]]. Or, leave your PHP as is and do this in your form instead.

<input type="checkbox" name="ExpressSaver" value="ExpressSaver">The Express Saver<br />
<input type="checkbox" name="PremiumDetail" value="PremiumDetail">The Premium Detail<br />

That would do it, I don't use checkbox arrays often and usually use names.

mbabuskov

6:02 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



When checkbox is not checked the POST does not contain the field at all. You should check whether field is set using isset().

I recommend you place all the field names into an array and then iterate over that.

foreach (array('nonautoExteriorWash',...etc. as $field)
if (isset($_POST[$field]))
$$field = mysql_realesape_string($_POST[$field])
else
$$field = '';

frella

6:21 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Thank you rocknbil and mbubuskov for the prompt responses!

However I guess I am still a bit confused.

rocknbil, you said not to use an array and mbabuskov you indicate using an array? So, which way is the easiest approach?
Sorry, I am not very good with php yet.

Here is what I am trying to do:
I have approximately 33 checkboxes on my html form page that are all related to available detail services. I want the checked services to show up in the MySQL database table I have created by somehow marking that particular service column. Right now all service columns show up blank, but the text field information does show.

mbabuskov

6:27 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



We were talking about different array. rocknbil was talking about you setting up fields in HTML as array and I was about creating array of field names to cut down the code size.

Maybe you should show us what your HTML looks like now before we confuse you any further.

frella

6:30 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



here is my complete html form:

<form action="../gdform.php" method="post"><table style="width: 100%" class="style33">
<input type="hidden" name="subject" value="Badge Form Submission" />
<input type="hidden" name="redirect" value="../thankyou.htm" />
<tr>
<td style="width: 204px" class="style35">First Name:</td>
<td>
<input name="firstname" type="text" style="width: 179px" /></td>
</tr>
<tr>
<td style="width: 204px" class="style35">Last Name:</td>
<td>
<input name="lastname" type="text" style="width: 179px" /></td>
</tr>
<tr>
<td style="width: 204px" class="style35">Company (if applicable):</td>
<td>
<input name="company" type="text" style="width: 179px" /></td>
</tr>
<tr>
<td style="width: 204px" class="style35">Phone Number:</td>
<td><input name="phone" type="text" style="width: 179px" /></td>
</tr>
<tr>
<td style="width: 204px" class="style35">E-Mail:</td>
<td><input name="email" type="text" style="width: 179px" /></td>
</tr>
<tr>
<td style="width: 204px" class="style35">Vehicle Make &amp; Model:</td>
<td>
<input name="vehicle" type="text" style="width: 179px" /></td>
</tr>
<tr>
<td style="width: 204px; height: 26px;" class="style35">Color of Vehicle:</td>
<td style="height: 26px">
<input name="color" type="text" style="width: 179px" /></td>
</tr>
<tr>
<td class="style34" colspan="2"><strong>Detail Service Desired&nbsp;
</strong>(check all that apply):</td>
</tr>
<tr>
<td class="style34" colspan="2">&nbsp;</td>
</tr></table>
<table><tr>
<td style="width: 658px" class="style33"><strong>
<span class="style35">SERVICES FOR AUTOMOBILES, TRUCKS, AND VANS:</span></strong><br />
<input type="checkbox" name="service[]" value="ExpressSaver"><strong><span class="style35">The Express Saver</span></strong>
<span class="style6">(Hand Wash &amp; Touchless Automatic Wash; Wheel Wash; Clear Coat Sealant; Hand Dry; Vacuum Seats, Floor Mats, Trunk; Windows Cleaned; Air Fragrance; Tire Shine)</span><br />
<input type="checkbox" name="service[]" value="PremiumDetail"><strong><span class="style6">The Premium Detail</span></strong>
<span class="style6">(<span class="style32"><em>The Express
Saver</em></span> PLUS: Whitewall Wheel Wash, Inside Q-Tip;
Armor-All Interior and Exterior)</span><br />
<input type="checkbox" name="service[]" value="WorksDetail"><strong><span class="style35">The Works Detail</span></strong>
<span class="style6">(<span class="style32"><em>The Premium Detail</em></span> PLUS: Hand Wax; Engine Wash)</span><br />
<input type="checkbox" name="service[]" value="UltimateDetail"><strong><span class="style35">Ultimate Detail</span></strong>
<span class="style6">(<span class="style32"><em>The Works Detail
</em></span>PLUS: Interior Shampoo; Paint Chip Repair)<br />
<br />
</span><strong><em><span class="style35">Additional Services for Automobiles, Trucks
and Vans:</span></em></strong><br />
<input type="checkbox" name="service[]" value="InteriorShampoo" class="style35"><span class="style35">Interior Shampoo<br />
</span>
<input type="checkbox" name="service[]" value="InteriorDetail" class="style35"><span class="style35">Interior Detail<br />
</span>
<input type="checkbox" name="service[]" value="FullSize" class="style35"><span class="style35">Full-Size Vehicle Surcharge<br />
</span>
<input type="checkbox" name="service[]" value="HandWax" class="style35"><span class="style35">Hand Wax Package<br />
</span>
<input type="checkbox" name="service[]" value="TarBug" class="style35"><span class="style35">Tar &amp; Bug Removal<br />
</span>
<input type="checkbox" name="service[]" value="Dent" class="style35"><span class="style35">Paintless Dent Repair<br />
</span>
<input type="checkbox" name="service[]" value="PaintChip" class="style35"><span class="style35">Paint Chip Repair<br />
</span>
<input type="checkbox" name="service[]" value="WindshieldChip" class="style35"><span class="style35">Windshield Chip Repair<br />
</span>
<input type="checkbox" name="service[]" value="Engine" class="style35"><span class="style35">Engine Cleaned<br />
</span>
<input type="checkbox" name="service[]" value="RimsBuff" class="style35"><span class="style35">Rims Buff</span><br />
<br />
<strong><span class="style35">SERVICES FOR BOATS, PWC, MOTORCYCLES, SNOWMOBILES,
RVs</span></strong><br />
<input type="checkbox" name="service[]" value="nonautoExteriorWash" class="style35"><span class="style35">Exterior Hand Wash<br />
</span>
<input type="checkbox" name="service[]" value="nonautoInteriorDetail" class="style35"><span class="style35">Boat/RV Interior Detail<br />
</span>
<input type="checkbox" name="service[]" value="nonautoInteriorVac" class="style35"><span class="style35">Boat/RV Interior Vacuum<br />
</span>
<input type="checkbox" name="service[]" value="nonautoWax" class="style35"><span class="style35">Hand Wax<br />
</span>
<input type="checkbox" name="service[]" value="nonautoOxidation" class="style35"><span class="style35">Buffing Oxidized Surface<br />
</span>
<input type="checkbox" name="service[]" value="nonautoAcid" class="style35"><span class="style35">Acid Wash<br />
</span>
<input type="checkbox" name="service[]" value="nonautoMotorcycleVinyl" class="style35"><span class="style35">Motorcycle Leather and Vinyl Cleaning<br />
</span>
<input type="checkbox" name="service[]" value="nonautoMotorcycleChrome" class="style35"><span class="style35">Motorcycle Chrome Cleaned and Polished<br />
</span>
<input type="checkbox" name="service[]" value="nonautoMotorcycleBug" class="style35"><span class="style35">Motorcycle Bug &amp; Tar Removal<br />
</span>
<input type="checkbox" name="service[]" value="nonautoRims" class="style35"><span class="style35">Snowmobile/PWC Detail</span><br />
</td>
</tr><tr>
<td style="width: 658px" class="style2">
<input name="Submit1" type="submit" value="Submit Form" class="style33" /></td>
</tr></table>
<tr>
<td style="width: 204px">
</form>

frella

6:32 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



sorry the first line is different
<form action="../insert.php" method="post">

the gdform.php is what I am using temporarily until I get the insert.php to work the way I want it to....

mbabuskov

6:50 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



In that case, read rocknbil's reply, it contains two possible solutions to your problem.

frella

6:54 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



I will try rocknbil's suggestion to use names and see what happens. Will I have to change any attributes in my database table? Again, right now I have the "services" columns set to VARCHAR....should this change at all?

I appreciate this forum very much! I have learned a lot from it and appreciate both of your time!

mbabuskov

7:15 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



probably not, but it's hard to tell without seeing the table structure.

frella

7:26 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Yes! It works and the values show up in my database table as VARCHAR. Thanks again!

Now I have another problem. How can I integrate GoDaddy's gdform script into my current php so that I also get the information submitted to an email and redirect to the "thank you" page I indicated on my html form page?

I just added the following to my php and I get the error message:
Cannot modify header information - headers already sent by....

here is my php with the gdform script added:
<?
include("dbinfo.inc.php");
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");


$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$company = mysql_real_escape_string($_POST['company']);
$phone = mysql_real_escape_string($_POST['phone']);
$email = mysql_real_escape_string($_POST['email']);
$vehicle = mysql_real_escape_string($_POST['vehicle']);
$color = mysql_real_escape_string($_POST['color']);
$ExpressSaver = mysql_real_escape_string($_POST['ExpressSaver']);
$PremiumDetail = mysql_real_escape_string($_POST['PremiumDetail']);
$WorksDetail = mysql_real_escape_string($_POST['WorksDetail']);
$UltimateDetail = mysql_real_escape_string($_POST['UltimateDetail']);
$InteriorShampoo = mysql_real_escape_string($_POST['InteriorShampoo']);
$InteriorDetail = mysql_real_escape_string($_POST['InteriorDetail']);
$FullSize = mysql_real_escape_string($_POST['FullSize']);
$HandWax = mysql_real_escape_string($_POST['HandWax']);
$TarBug = mysql_real_escape_string($_POST['TarBug']);
$Dent = mysql_real_escape_string($_POST['Dent']);
$PaintChip = mysql_real_escape_string($_POST['PaintChip']);
$WindshieldChip = mysql_real_escape_string($_POST['WindshieldChip']);
$Engine = mysql_real_escape_string($_POST['Engine']);
$RimsBuff = mysql_real_escape_string($_POST['RimsBuff']);
$nonautoExteriorWash = mysql_real_escape_string($_POST['nonautoExteriorWash']);
$nonautoInteriorDetail = mysql_real_escape_string($_POST['nonautoInteriorDetail']);
$nonautoInteriorVac = mysql_real_escape_string($_POST['nonautoInteriorVac']);
$nonautoWax = mysql_real_escape_string($_POST['nonautoWax']);
$nonautoOxidation = mysql_real_escape_string($_POST['nonautoOxidation']);
$nonautoAcid = mysql_real_escape_string($_POST['nonautoAcid']);
$nonautoMotorcycleVinyl = mysql_real_escape_string($_POST['nonautoMotorcycleVinyl']);
$nonautoMotorcycleChrome = mysql_real_escape_string($_POST['nonautoMotorcycleChrome']);
$nonautoMotorcycleBug = mysql_real_escape_string($_POST['nonautoMotorcycleBug']);
$nonautoRims = mysql_real_escape_string($_POST['nonautoRims']);

$query = "INSERT INTO Detailing VALUES ('','$firstname','$lastname','$company','$phone','$email','$vehicle','$color', '$ExpressSaver', '$PremiumDetail', '$WorksDetail', '$UltimateDetail', '$InteriorShampoo', '$InteriorDetail', '$FullSize', '$HandWax', '$TarBug', '$Dent', '$PaintChip', '$WindshieldChip', '$Engine', '$RimsBuff', '$nonautoExteriorWash', '$nonautoInteriorDetail', '$nonautoInteriorVac', '$nonautoWax', '$nonautoOxidation', '$nonautoAcid', '$nonautoMotorcycleVinyl', '$nonautoMotorcycleChrome', '$nonautoMotorcycleBug', '$nonautoRims')";
mysql_query($query);

mysql_close();
$request_method = $_SERVER["REQUEST_METHOD"];
if($request_method == "GET"){
$query_vars = $_GET;
} elseif ($request_method == "POST"){
$query_vars = $_POST;
}
reset($query_vars);
$t = date("U");

$file = $_SERVER['DOCUMENT_ROOT'] . "/../data/gdform_" . $t;
$fp = fopen($file,"w");
while (list ($key, $val) = each ($query_vars)) {
fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\n");
fputs($fp,"$val\n");
fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\n");
if ($key == "redirect") { $landing_page = $val;}
}
fclose($fp);
if ($landing_page != ""){
header("Location: [".$_SERVER["HTTP_HOST"]."...]
} else {
header("Location: [".$_SERVER["HTTP_HOST"]."...]
}


?>

mbabuskov

9:47 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



That error message shows up when some output is sent back before the header() function. You should take a look and see if there is some output or warning messages and maybe set error_reporting(0) at start of the script.

Alternatively, you could place ob_start() at the start of the script to make sure no output is sent before header() function.

frella

9:50 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Thank you again mbabuskov! I switched to formmail.php from Tectite.com and it works great and I can also submit data to the database.

rocknbil

5:20 pm on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you said not to use an array and mbabuskov you indicate using an array?


I didn't say **not** to use an array for checkboxes - I said **I** don't use them often and prefer named elements. Either approach is valid.