Re: Problem:



On Oct 29, 5:21 am, Gordon <gordon.mc...@xxxxxxxxxxxx> wrote:
On Oct 28, 4:55 am, rohit reja <rejarohit2...@xxxxxxxxx> wrote:

Hi everyone

I am developing a website..the form in my php page has a lot of
checkboxes..to set differebt options.
Now how do i write mysql query for that form according to checkbox
selected..is there any shorter way.
Plz help

Prepared statements and a foreach over your checkboxes.

foreach ($checkboxes as &$thisCheck)
{
    $thisCheck?
        $thicCheck = 1:
        $thisCheck = 0;

}

$statement = $db -> prepare (UPDATE table SET item1=?, item2=? item3=?
WHERE id=?);
$statement -> execute (array ($checkboxes [0], $checkboxes [1],
$checkboxes [2], $id));

This assumes that they're using that as their Database interface.
Which is bad.
I would go with sheldonlg's idea:

Build your sql query with sequential if statements.

$sql = "SELECT blah FROM foobar WHERE doe='rae';

Only, instead of using the whole "if( $check1 == 'on')", the default
way checkboxes work (at least with Apache and PHP), is that the ones
checked will be in whichever request format you use ($_GET or $_POST)
and the unchecked ones won't, assuming you set up the checkboxes
correctly (giving them the same name and treating them as an array.)

To demonstrate:
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="checkbox" name="boxes[]" value="1" />
<input type="checkbox" name="boxes[]" value="2" />
<input type="checkbox" name="boxes[]" value="3" />
<input type="checkbox" name="boxes[]" value="4" />
<input type="checkbox" name="boxes[]" value="5" />
<input type="checkbox" name="boxes[]" value="6" />
<input type="checkbox" name="boxes[]" value="7" />
<input type="checkbox" name="boxes[]" value="8" />
<input type="checkbox" name="boxes[]" value="9" />
<input type="submit" value="Submit" />
</form>
<?php
if( array_key_exists( "boxes", $_GET ) )
{
foreach( $_GET['boxes'] as $box => $value )
echo "Box: $box, Value: $value<br />";
}
?>
</body>
</html>
.



Relevant Pages

  • Re: Problem:
    ... checkboxes..to set differebt options. ... Now how do i write mysql query for that form according to checkbox ... Prepared statements and a foreach over your checkboxes. ...
    (comp.lang.php)
  • Re: Is there any INPUT type that is stored in an array on PHP?
    ... Multiple selections must be ... In PHP I want to do: ... foreach { ... But using Checkboxes doesn't work, I've tried putting them with the ...
    (comp.lang.php)
  • Re: Is there any INPUT type that is stored in an array on PHP?
    ... Multiple selections must be ... In PHP I want to do: ... foreach { ... Append to the names of the checkboxes, ...
    (comp.lang.php)
  • Re: Beginger Web Form Question
    ... What I want to do is create a document with checkboxes that all the ... So far it seems like I will have to install PHP and mySQL into the Mac ... There's a database called SQLite that's very easy to install and yet is ...
    (alt.html)
  • Re: Query string before parsing
    ... software generator which creates multiple checkboxes using the same ... pass the correct POST values. ... Actually this may be a bug for PHP to fix. ... This is what w3 has to say about checkboxes: ...
    (comp.lang.php)