...

View Full Version : Inserting Multiple Checkbox Values Into Database



ben1390
03-10-2010, 11:39 AM
Hi All, I have this form with checkboxes, and would like the values of all the checkboxes ticked to be interted into a single field into the database, however it does not do this correctly, can anyone see where I am going wrong with this?


<!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>Market Research</title>
</head>
<body>
<form method="post" action="process.php">
<p>Please indicate which industrial sector you conduct business within:</p>
<input type="radio" name="1" value="Primary" />Primary<br />
<input type="radio" name="1" value="Secondary" />Secondary<br />
<input type="radio" name="1" value="Tertiary" />Tertiary<br />
<input type="radio" name="1" value="Quaternary" />Quaternary

<p>1) On a scale of 1 to 5, how much do you value your website as a business asset?</p>
<input type="radio" name="2" value="1" />1
<input type="radio" name="2" value="2" />2
<input type="radio" name="2" value="3" />3
<input type="radio" name="2" value="4" />4
<input type="radio" name="2" value="5" />5

<p>2) On a scale of 1 to 5, how well do you understand the role of your company's website?</p>
<input type="radio" name="3" value="1" />1
<input type="radio" name="3" value="2" />2
<input type="radio" name="3" value="3" />3
<input type="radio" name="3" value="4" />4
<input type="radio" name="3" value="5" />5

<p>3) Do you create, plan and execute a strategically thought out marketing plan for your company at least once a year?</p>
<input type="radio" name="4" value="Yes" />Yes
<input type="radio" name="4" value="No" />No

<p>4) On a scale of 1 to 5, how much business do you think your website generates for you?</p>
<input type="radio" name="5" value="1" />1
<input type="radio" name="5" value="2" />2
<input type="radio" name="5" value="3" />3
<input type="radio" name="5" value="4" />4
<input type="radio" name="5" value="5" />5

<p>5) Which of the following do you think should be your websites number one task?</p>
<input type="radio" name="6" value="To act as an accurate visual presentation of your company" />To act as an accurate visual presentation of your company<br />
<input type="radio" name="6" value="Attract as many new leads as possible" />Attract as many new leads as possible<br />
<input type="radio" name="6" value="Inform your target market of the servies/products you offer" />Inform your target market of the servies/products you offer

<p>6) On a scale of 1 to 5, how relevant to your business do you think Search Engine Optimisation will become in the next 12 months?</p>
<input type="radio" name="7" value="1" />1
<input type="radio" name="7" value="2" />2
<input type="radio" name="7" value="3" />3
<input type="radio" name="7" value="4" />4
<input type="radio" name="7" value="5" />5

<p>7) On a scale of 1 to 5, how relavent to your business' success will your website be during the next 12 months?</p>
<input type="radio" name="8" value="1" />1
<input type="radio" name="8" value="2" />2
<input type="radio" name="8" value="3" />3
<input type="radio" name="8" value="4" />4
<input type="radio" name="8" value="5" />5

<p>8) Please indicate which products below you think you would most benefit from:</p>
<input type="radio" name="9" value="Search Engine Optimisation" />Search Engine Optimisation<br />
<input type="radio" name="9" value="A complete new website" />A complete new website<br />
<input type="radio" name="9" value="Consultancy on how to better use your online presence" />Consultancy on how to better use your online presence<br />
<input type="radio" name="9" value="Consultancy on Search Engine Optimisation and Internet Marketing" />Consultancy on Search Engine Optimisation and Internet Marketing

<p>9) On a scale of 1 to 5, to what degree would you say you understand the term "digital marketing"?</p>
<input type="radio" name="10" value="1" />1
<input type="radio" name="10" value="2" />2
<input type="radio" name="10" value="3" />3
<input type="radio" name="10" value="4" />4
<input type="radio" name="10" value="5" />5

<p>10) Please indicate, by ticking the appropriate boxes, the elements you think that makes a strong web design firm:</p>
<input type="checkbox" name="11[]" value="Strong portfolio" />Strong portfolio<br />
<input type="checkbox" name="11[]" value="Wide range of services" />Wide range of services<br />
<input type="checkbox" name="11[]" value="Rapid turnaround of projects" />Rapid turnaround of projects<br />
<input type="checkbox" name="11[]" value="Design awards for their websites" />Design awards for their websites<br />
<input type="checkbox" name="11[]" value="Revavant accreditations and awards" />Revavant accreditations and awards<br />
<input type="checkbox" name="11[]" value="Evidence of results" />Evidence of results<br />
<input type="checkbox" name="11[]" value="Frequent communication with you" />Frequent communication with you<br />
<input type="checkbox" name="11[]" value="Strong differentiation from completion" />Strong differentiation from completion

<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
<?php
$connection = mysql_connect("localhost","username","password") or die ('Could not connect: ' . mysql_error());
$question1 = $_POST['1'];
$question2 = $_POST['2'];
$question3 = $_POST['3'];
$question4 = $_POST['4'];
$question5 = $_POST['5'];
$question6 = $_POST['6'];
$question7 = $_POST['7'];
$question8 = $_POST['8'];
$question9 = $_POST['9'];
$question10 = $_POST['10'];
$question11 = serialize($_POST['11']);

mysql_select_db("database_name", $connection);
mysql_query("INSERT INTO entries (question1, question2, question3, question4, question5, question6, question7, question8, question9, question10, question11) VALUES ('$question1', '$question2', '$question3', '$question4', '$question5', '$question6', '$question7', '$question8', '$question9', '$question10', '$question11')");
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
?>

mlseim
03-10-2010, 12:36 PM
You have to use arrays ...

Example:

<p>Please indicate which industrial sector you conduct business within:</p>
<input type="radio" name="r1[]" value="Primary" />Primary<br />
<input type="radio" name="r1[]" value="Secondary" />Secondary<br />
<input type="radio" name="r1[]" value="Tertiary" />Tertiary<br />
<input type="radio" name="r1[]" value="Quaternary" />Quaternary

Then in PHP, you read the array

I don't have time now to post more, but you can find examples using Google,
or someone else will provide some examples.

ben1390
03-12-2010, 09:29 AM
Thank you for your reply, I have now done this, however the word Array appears in the database field, any ideas on how to solve this?

mlseim
03-12-2010, 12:23 PM
Reprint your script ... what you have now.

ben1390
03-12-2010, 01:35 PM
<!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>Market Research</title>
</head>
<body>
<form method="post" action="process.php">
<p>Please indicate which industrial sector you conduct business within:</p>
<input type="radio" name="1[]" value="Primary" />Primary<br />
<input type="radio" name="1[]" value="Secondary" />Secondary<br />
<input type="radio" name="1[]" value="Tertiary" />Tertiary<br />
<input type="radio" name="1[]" value="Quaternary" />Quaternary

<p>1) On a scale of 1 to 5, how much do you value your website as a business asset?</p>
<input type="radio" name="2[]" value="1" />1
<input type="radio" name="2[]" value="2" />2
<input type="radio" name="2[]" value="3" />3
<input type="radio" name="2[]" value="4" />4
<input type="radio" name="2[]" value="5" />5

<p>2) On a scale of 1 to 5, how well do you understand the role of your company's website?</p>
<input type="radio" name="3[]" value="1" />1
<input type="radio" name="3[]" value="2" />2
<input type="radio" name="3[]" value="3" />3
<input type="radio" name="3[]" value="4" />4
<input type="radio" name="3[]" value="5" />5

<p>3) Do you create, plan and execute a strategically thought out marketing plan for your company at least once a year?</p>
<input type="radio" name="4[]" value="Yes" />Yes
<input type="radio" name="4[]" value="No" />No

<p>4) On a scale of 1 to 5, how much business do you think your website generates for you?</p>
<input type="radio" name="5[]" value="1" />1
<input type="radio" name="5[]" value="2" />2
<input type="radio" name="5[]" value="3" />3
<input type="radio" name="5[]" value="4" />4
<input type="radio" name="5[]" value="5" />5

<p>5) Which of the following do you think should be your websites number one task?</p>
<input type="radio" name="6[]" value="To act as an accurate visual presentation of your company" />To act as an accurate visual presentation of your company<br />
<input type="radio" name="6[]" value="Attract as many new leads as possible" />Attract as many new leads as possible<br />
<input type="radio" name="6[]" value="Inform your target market of the servies/products you offer" />Inform your target market of the servies/products you offer

<p>6) On a scale of 1 to 5, how relevant to your business do you think Search Engine Optimisation will become in the next 12 months?</p>
<input type="radio" name="7[]" value="1" />1
<input type="radio" name="7[]" value="2" />2
<input type="radio" name="7[]" value="3" />3
<input type="radio" name="7[]" value="4" />4
<input type="radio" name="7[]" value="5" />5

<p>7) On a scale of 1 to 5, how relavent to your business' success will your website be during the next 12 months?</p>
<input type="radio" name="8[]" value="1" />1
<input type="radio" name="8[]" value="2" />2
<input type="radio" name="8[]" value="3" />3
<input type="radio" name="8[]" value="4" />4
<input type="radio" name="8[]" value="5" />5

<p>8) Please indicate which products below you think you would most benefit from:</p>
<input type="radio" name="9[]" value="Search Engine Optimisation" />Search Engine Optimisation<br />
<input type="radio" name="9[]" value="A complete new website" />A complete new website<br />
<input type="radio" name="9[]" value="Consultancy on how to better use your online presence" />Consultancy on how to better use your online presence<br />
<input type="radio" name="9[]" value="Consultancy on Search Engine Optimisation and Internet Marketing" />Consultancy on Search Engine Optimisation and Internet Marketing

<p>9) On a scale of 1 to 5, to what degree would you say you understand the term "digital marketing"?</p>
<input type="radio" name="10[]" value="1" />1
<input type="radio" name="10[]" value="2" />2
<input type="radio" name="10[]" value="3" />3
<input type="radio" name="10[]" value="4" />4
<input type="radio" name="10[]" value="5" />5

<p>10) Please indicate, by ticking the appropriate boxes, the elements you think that makes a strong web design firm:</p>
<input type="checkbox" name="11[]" value="Strong portfolio" />Strong portfolio<br />
<input type="checkbox" name="11[]" value="Wide range of services" />Wide range of services<br />
<input type="checkbox" name="11[]" value="Rapid turnaround of projects" />Rapid turnaround of projects<br />
<input type="checkbox" name="11[]" value="Design awards for their websites" />Design awards for their websites<br />
<input type="checkbox" name="11[]" value="Revavant accreditations and awards" />Revavant accreditations and awards<br />
<input type="checkbox" name="11[]" value="Evidence of results" />Evidence of results<br />
<input type="checkbox" name="11[]" value="Frequent communication with you" />Frequent communication with you<br />
<input type="checkbox" name="11[]" value="Strong differentiation from completion" />Strong differentiation from completion

<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>
<?php
$connection = mysql_connect("localhost","username","password") or die ('Could not connect: ' . mysql_error());
$question1 = $_POST['1'];
$question2 = $_POST['2'];
$question3 = $_POST['3'];
$question4 = $_POST['4'];
$question5 = $_POST['5'];
$question6 = $_POST['6'];
$question7 = $_POST['7'];
$question8 = $_POST['8'];
$question9 = $_POST['9'];
$question10 = $_POST['10'];
$question11 = serialize($_POST['11']);

mysql_select_db("database_name", $connection);
mysql_query("INSERT INTO entries (question1, question2, question3, question4, question5, question6, question7, question8, question9, question10, question11) VALUES ('$question1', '$question2', '$question3', '$question4', '$question5', '$question6', '$question7', '$question8', '$question9', '$question10', '$question11')");
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
?>

DaiLaughing
03-12-2010, 02:51 PM
As the variables (e.g. $question1) are arrays PHP is returning that fact. I don't understand why you would want the data in a single field because that's not what databases do. Fields should hold one piece of data only. However if you really want to swap:


$question1 = $_POST['1'];

with something like:


$question1 = implode(",",$_POST['1']);

That will give you the values in one variable separated by commas (you could use any character).

mlseim
03-12-2010, 03:06 PM
oh ... my bad.

The array only needs to be for your checkboxes (not the radio buttons).

Your radio buttons will look like this:
<p>Please indicate which industrial sector you conduct business within:</p>
<input type="radio" name="1" value="Primary" />Primary<br />
<input type="radio" name="1" value="Secondary" />Secondary<br />
<input type="radio" name="1" value="Tertiary" />Tertiary<br />
<input type="radio" name="1" value="Quaternary" />Quaternary

You checkboxes will look like:
<p>10) Please indicate, by ticking the appropriate boxes, the elements you think that makes a strong web design firm:</p>
<input type="checkbox" name="11[]" value="Strong portfolio" />Strong portfolio<br />
<input type="checkbox" name="11[]" value="Wide range of services" />Wide range of services<br />
etc...

Then, in your script, you will have to process the check boxes, as they will be an array.

Here's a way to test it ... only the boxes checked will appear in the array.

$checkboxes=$_POST['11'];
while (list ($key,$val) = @each ($checkbox)) {
echo "$val,";
}

ben1390
03-17-2010, 10:37 AM
Thank you this has worked, however where I have the radio buttons and text areas with the same name, the value of the radio buttons is not being inserted into the database, any ideas why this is happening?

mlseim
03-17-2010, 12:23 PM
Form variables can't have the same name.
But I don't see any text boxes on your form (post #5)

ben1390
03-17-2010, 02:35 PM
Sorry, here is the new code, questions 5 and 9 have radio buttons as well as text areas, however the values of the radio buttons are not being entered into the database


<!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>Market Research</title>
</head>
<body>
<form method="post" action="process.php">
<p>Please indicate which industrial sector you conduct business within:</p>
<input type="radio" name="1" value="Primary" />Primary<br />
<input type="radio" name="1" value="Secondary" />Secondary<br />
<input type="radio" name="1" value="Tertiary" />Tertiary<br />
<input type="radio" name="1" value="Quaternary" />Quaternary

<p>1) On a scale of 1 to 5, how much do you value your website as a business asset?</p>
<input type="radio" name="2" value="1" />1
<input type="radio" name="2" value="2" />2
<input type="radio" name="2" value="3" />3
<input type="radio" name="2" value="4" />4
<input type="radio" name="2" value="5" />5

<p>2) On a scale of 1 to 5, how well do you understand the role of your company's website?</p>
<input type="radio" name="3" value="1" />1
<input type="radio" name="3" value="2" />2
<input type="radio" name="3" value="3" />3
<input type="radio" name="3" value="4" />4
<input type="radio" name="3" value="5" />5

<p>3) Do you create, plan and execute a strategically thought out marketing plan for your company at least once a year?</p>
<input type="radio" name="4" value="Yes" />Yes
<input type="radio" name="4" value="No" />No

<p>4) On a scale of 1 to 5, how much business do you think your website generates for you?</p>
<input type="radio" name="5" value="1" />1
<input type="radio" name="5" value="2" />2
<input type="radio" name="5" value="3" />3
<input type="radio" name="5" value="4" />4
<input type="radio" name="5" value="5" />5

<p>5) Which of the following do you think should be your websites number one task?</p>
<input type="radio" name="6" value="To act as an accurate visual presentation of your company" />To act as an accurate visual presentation of your company<br />
<input type="radio" name="6" value="Attract as many new leads as possible" />Attract as many new leads as possible<br />
<input type="radio" name="6" value="Inform your target market of the servies/products you offer" />Inform your target market of the servies/products you offer<br />
Other (please specify) <textarea name="6"></textarea>

<p>6) On a scale of 1 to 5, how relevant to your business do you think Search Engine Optimisation will become in the next 12 months?</p>
<input type="radio" name="7" value="1" />1
<input type="radio" name="7" value="2" />2
<input type="radio" name="7" value="3" />3
<input type="radio" name="7" value="4" />4
<input type="radio" name="7" value="5" />5

<p>7) On a scale of 1 to 5, how relavent to your business' success will your website be during the next 12 months?</p>
<input type="radio" name="8" value="1" />1
<input type="radio" name="8" value="2" />2
<input type="radio" name="8" value="3" />3
<input type="radio" name="8" value="4" />4
<input type="radio" name="8" value="5" />5

<p>8) Please indicate which products below you think you would most benefit from:</p>
<input type="radio" name="9" value="Search Engine Optimisation" />Search Engine Optimisation<br />
<input type="radio" name="9" value="A complete new website" />A complete new website<br />
<input type="radio" name="9" value="Consultancy on how to better use your online presence" />Consultancy on how to better use your online presence<br />
<input type="radio" name="9" value="Consultancy on Search Engine Optimisation and Internet Marketing" />Consultancy on Search Engine Optimisation and Internet Marketing

<p>9) On a scale of 1 to 5, to what degree would you say you understand the term "digital marketing"?</p>
<input type="radio" name="10" value="1" />1
<input type="radio" name="10" value="2" />2
<input type="radio" name="10" value="3" />3
<input type="radio" name="10" value="4" />4
<input type="radio" name="10" value="5" />5
Other (please specify) <textarea name="10"></textarea>

<p>10) Please indicate, by ticking the appropriate boxes, the elements you think that makes a strong web design firm:</p>
<input type="checkbox" name="11[]" value="Strong portfolio" />Strong portfolio<br />
<input type="checkbox" name="11[]" value="Wide range of services" />Wide range of services<br />
<input type="checkbox" name="11[]" value="Rapid turnaround of projects" />Rapid turnaround of projects<br />
<input type="checkbox" name="11[]" value="Design awards for their websites" />Design awards for their websites<br />
<input type="checkbox" name="11[]" value="Revavant accreditations and awards" />Revavant accreditations and awards<br />
<input type="checkbox" name="11[]" value="Evidence of results" />Evidence of results<br />
<input type="checkbox" name="11[]" value="Frequent communication with you" />Frequent communication with you<br />
<input type="checkbox" name="11[]" value="Strong differentiation from completion" />Strong differentiation from completion

<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

mlseim
03-17-2010, 04:30 PM
You'll have to change these:

<textarea name="6"></textarea>
<textarea name="10"></textarea>


To these (put a 't' in front of the number) ...

<textarea name="t6"></textarea>
<textarea name="t10"></textarea>

Then, add those variable names to your PHP form ...
$t6=$_POST['t6'];
$t10=$_POST['t10'];

And work with them as the names you've given them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum