Re: Problem:
- From: Michael Sherwood <coolhand2@xxxxxxxxx>
- Date: Wed, 29 Oct 2008 12:20:37 -0700 (PDT)
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>
.
- References:
- Problem:
- From: rohit reja
- Re: Problem:
- From: Gordon
- Problem:
- Prev by Date: Re: Manually make a POST request?
- Next by Date: Re: IE6 strange behaviour with fwrite calls
- Previous by thread: Re: Problem:
- Next by thread: Re: PHP has encountered an access violation...
- Index(es):
Relevant Pages
|