By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

Problem:

P: n/a
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
Oct 28 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tue, 28 Oct 2008 05:55:10 +0100, rohit reja <re***********@gmail.com>
wrote:
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.
Shorter then what?
--
Rik
Oct 28 '08 #2

P: n/a
rohit reja 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
Build your sql query with sequential if statements.

$sql = "SELECT blah FROM foobar WHERE doe='rae';
if ($check1 == 'on') $sql .= ' AND blahblah';
.....
.....
Oct 28 '08 #3

P: n/a
On 28 Oct, 04:55, rohit reja <rejarohit2...@gmail.comwrote:
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
Shorter way than what?
Oct 28 '08 #4

P: n/a
On Oct 29, 1:17*am, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 28 Oct, 04:55, rohit reja <rejarohit2...@gmail.comwrote:
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

Shorter way than what?
Shorter than using ifelse statements!!
Oct 29 '08 #5

P: n/a
On Oct 28, 4:55*am, rohit reja <rejarohit2...@gmail.comwrote:
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));
Oct 29 '08 #6

P: n/a
On Oct 29, 5:21*am, Gordon <gordon.mc...@ntlworld.comwrote:
On Oct 28, 4:55*am, rohit reja <rejarohit2...@gmail.comwrote:
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>
Oct 29 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.