RE: How to process a query form with >>CHECKBOX<< RADIO not checkbox !
| From: | Neil Smith [MVP, Digital media] | Date: | Tue, 28 Dec 2004 12:26:33 +0000 |
| Subject: | RE: How to process a query form with >>CHECKBOX<< RADIO not checkbox ! | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-37886@lists.php.net to get a copy of this message | ||
At 01:23 28/12/2004 +0000, you wrote:
Date: Mon, 27 Dec 2004 17:22:49 -0800 (PST) From: S Kumar <ps_php@yahoo.com> To: php-db@lists.php.net, php-general@lists.php.net MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: How to process a query form with CHECKBOX Please help <DIV>Age group</DIV> <DIV>Young<input type="checkbox" name="" value=""></DIV> <DIV>Middle<input type="checkbox" name="" value=""></DIV> <DIV>Old<input type="checkbox" name="" value=""></DIV> Problem: I want to capture the user options and create an SQL statement:You need to make your <form action="post"> You also need to use a "radio" button because a patient may not be young *and* old for example. Radio buttons offer an 'either-or' selection, where a checkbox offers an 'and' selection. You also need to give the radio buttons a name (the same for each one in a group of options) eg <input type="radio" name="age" id="age1" value="young" /> Young <input type="radio" name="age" id="age2" value="middle" /> Middle <input type="radio" name="age" id="age3" value="old" /> Old When you submit the form, PHP will have a variable in $_POST array which contains the value of the selection. It will be called the same as the name attribute in your input (radio) button : $allowed_ages = array("young", "middle", "old"); $age_group = $_POST["age"];
// We should check that the value is of the types we allow into the database // this will prevent form spoofing because we're checking for an exact matchif (in_array($age_group, $allowed_ages)) {
// At this point we should make a connection to the database.
// Well make the resource called "$link" and it will be used below
$link=mysql_connect("my_server", "my_username", "my_password");
// There may be multiple *databases* on the server, each with lots of *tables*
mysql_select_db("my_database_name", $link);
$query="INSERT INTO patient_data
(age_group)
VALUES ('".$age_group."');
// Execute query
mysql_query($query, $link);
// Check for success
if (mysql_error($link)) {
// Something went wrong with the query
print("Error in query : ".$query);
} else {
// Print success
print("Your record was added");
}
} else {
// We were receiving a spoofed form entry which could break the database
print("Value is not acceptable");
exit;
}
Select age_group from patient_data where age_group = young and old ;Just use SELECT * FROM patient_data WHERE age_group IN (young, old) There would be no point in selecting age_group because all you'll get is a list of young-old-old-old-young etc without any useful information. If you wanted to know *how many* young and old (a summary) you'd use GROUP BY eg SELECT age_group, COUNT(age_group) FROM patient_data WHERE age_group IN (young, old) GROUP BY age_group so you'd get ========== young 15
old 24