shane
shane

Reputation: 134

Have HTML form with text and checkbox - how do I post checkbox responses to Mysql table

I have an HTML form with several various text and checkbox fields. I have the text fields posting to the correct table but the checkbox responses are not posting at all (intended to post to a separate table).

Here's my HTML form:

    <!Doctype html>
    <html>
    <?php include 'C:\xampp\htdocs\paxdb\head.php'; 
    include 'config/menu.php';?> 
    <div id="dataentry">
    <!--This section is the demographic text field area-->
    <form method="post" action="dataentered.php">
    First Name:&nbsp;<input type="text" id="First_Name" name="First_Name"/></br>
    </br>
    Last Name:&nbsp;<input type="text" id="Last_Name" name="Last_Name"/></br>
    </br>
    E-mail:&nbsp;<input type="text" id="email" name="email"/></br>
    </br>

    <!--This section is the age range checkbox selection area-->
    <p><u><b>Age Range</b></u></p>
    <input type="checkbox" name="age[]" id="20-25" value="1"/>&nbsp;20-25</br>
    <input type="checkbox" name="age[]" id="26-30" value="1"/>&nbsp;26-30</br>
    <input type="checkbox" name="age[]" id="31-35" value="1"/>&nbsp;31-35</br>
    <input type="checkbox" name="age[]" id="36-40" value="1"/>&nbsp;36-40</br>
    <input type="checkbox" name="age[]" id="41-45" value="1"/>&nbsp;41-45</br>
    </div>

<p><u><b>What City or region would you like to visit in the US or Canada?:</b></u></p>
<textarea name="comment2" rows="4" cols="50"></textarea>

<?php include 'footer.php';?>       
        </div>
    </body>
</html>

and here is the PHP code I am trying but only have the text fields working:

<html>
<?php
$host="localhost";
$username="someusername";
$password="somepassword";
$dbname="somedbname";

$dbc = mysql_connect($host, $username, $password, $dbname);
if (!$dbc)
{
    die('Error connecting to MySQL server' . mysql_error());
    }
mysql_select_db($dbname, $dbc);

//send pax data to pax database table
$first_name=$_POST['First_Name'];   
$last_name=$_POST['Last_Name'];
$email=$_POST['email'];

mysql_query("INSERT INTO pax (First_Name, Last_Name, email)
VALUES('$first_name','$last_name','$email')"); 

//send age checkbox data to age database table
$age = $_POST['age'];
foreach($age as $range) mysql_query("INSERT INTO age ($age) VALUES ('$range')") or die (mysql_error());


mysql_close($dbc);

Any help is appreciated.

EDIT To clarify: the mysql table 'age' has the following fields: age_id (key), pax_id (index for the text field data at the beginning of the form), 20-25 26-30 and so on through the age ranges.

Upvotes: 0

Views: 2445

Answers (2)

Riten
Riten

Reputation: 38

I have edited the code for you

<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\paxdb\head.php'; 
include 'config/menu.php';?> 
<div id="dataentry">
<!--This section is the demographic text field area-->
<form method="post" action="dataentered.php">
First Name:&nbsp;<input type="text" id="First_Name" name="First_Name"/></br>
</br>
Last Name:&nbsp;<input type="text" id="Last_Name" name="Last_Name"/></br>
</br>
E-mail:&nbsp;<input type="text" id="email" name="email"/></br>
</br>

<!--This section is the age range checkbox selection area-->
<p><u><b>Age Range</b></u></p>
<!-- Change your checkbox value -->
<input type="checkbox" name="age[]" id="20-25" value="20-25"/>&nbsp;20-25</br>
<input type="checkbox" name="age[]" id="26-30" value="26-30"/>&nbsp;26-30</br>
<input type="checkbox" name="age[]" id="31-35" value="31-35"/>&nbsp;31-35</br>
<input type="checkbox" name="age[]" id="36-40" value="36-40"/>&nbsp;36-40</br>
<input type="checkbox" name="age[]" id="41-45" value="41-45"/>&nbsp;41-45</br>
</div>

<p><u><b>What City or region would you like to visit in the US or Canada?:</b></u></p>
<textarea name="comment2" rows="4" cols="50"></textarea><br/>
<input type="submit" name="submit" value="Submit"/>
<?php include 'footer.php';?>       
</div>
</body>
</html>

You cannot insert the array($age) directly into the database as you have done in INSERT statement...One of the solution that i did is convert the array into string and then insert it into database as shown...

<?php
$host="localhost";
$username="someusername";
$password="somepassword";
$dbname="somedbname";

$dbc = mysql_connect($host, $username, $password, $dbname);
if (!$dbc)
{
    die('Error connecting to MySQL server' . mysql_error());
}
mysql_select_db($dbname, $dbc);

//send pax data to pax database table
$first_name=$_POST['First_Name'];   
$last_name=$_POST['Last_Name'];
$email=$_POST['email'];

mysql_query("INSERT INTO pax (First_Name, Last_Name, email)
VALUES('$first_name','$last_name','$email')"); 

//send age checkbox data to age database table
$age = $_POST['age'];
$my_range = "";
foreach($age as $range)
$my_range = $my_range . $range . " ";
//You have written this query wrong
mysql_query("INSERT INTO age(age) VALUES ('$my_range')") or die (mysql_error());

mysql_close($dbc);

While retrieving it from the database you can use expode() to get each age range...Here is the sample code

$range_string = "20-25 26-30 31-35";
$range_array = explode(" ", $range_string);
echo $range_array [0]; // 20-25
echo $range_array [1]; // 26-30
echo $range_array [2]; // 31-35

You might also want to look at http://www.html-form-guide.com/php-form/php-form-checkbox.html for some information on checkboxes...

Hope this helps

Upvotes: 0

Kristian
Kristian

Reputation: 21830

before you try to actually run mysql queries, you should test to make sure the queries youre constructing have the correct information in them.

You could do something like this to quickly check:

foreach($age as $range) {
    print "INSERT INTO age ($age) VALUES ('$range')";
}

Also, you shouldn't be sticking $_POST data straight into your queries, someone could easily write SQL code into a field and delete your database. Look up the topic of "mysql input sanitation"

Upvotes: 1

Related Questions