RE: How to process a query form with >>CHECKBOX<< RADIO not checkbox !

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 01:23 28/12/2004 +0000, you wrote:
Date: Mon, 27 Dec 2004 17:22:49 -0800 (PST)
From: S Kumar <ps_php@xxxxxxxxx>
To: php-db@xxxxxxxxxxxxx, php-general@xxxxxxxxxxxxx
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 match
if (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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux