SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: Checkbox and MySQL's ENUM field
-
Sep 17, 2003, 06:22 #1
- Join Date
- Nov 2001
- Location
- United Kingdom
- Posts
- 171
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Checkbox and MySQL's ENUM field
Hi there,
I have a checkboxs like the one below in my form page, to be used with MySQL fields set to ENUM('Y','N') NOT NULL DEFAULT 'N'.
Code:<input name="$name" type="checkbox" value="Y" />
Rio
-
Sep 17, 2003, 08:03 #2
- Join Date
- Jan 2003
- Location
- Munich, Germany
- Posts
- 1,391
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If a checkbox is not activated (checked) it does not pass a value (as if it would not exist), so only if it's checked you get a value (Y in your case).
We are the Borg. Resistance is futile. Prepare to be assimilated.
I'm Pentium of Borg.Division is futile.Prepare to be approximated.
-
Sep 17, 2003, 08:25 #3
- Join Date
- Nov 2001
- Location
- United Kingdom
- Posts
- 171
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Even when the default value is set 'N' ?
That's being the case, do I have to tweak my PHP script to set the value of $name to 'N' ?
-
Sep 17, 2003, 09:57 #4
- Join Date
- Jan 2003
- Location
- Munich, Germany
- Posts
- 1,391
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The field should stay at N as long as no other value is submitted. Once it was Y it doesn't change to N again.
For doing this, you need a little PHP scripting.
Here's an excerpt from my script:
PHP Code:/**
* If variable is set, set it to the set_checked otherwise set it to the
* unset_checked. Used to handle checkboxes when you are expecting them from
* a form.
*/
function check_checked(&$var, $set_checked = 1, $unset_checked = 0)
{
if (empty($var))
{
$var = $unset_checked;
}
else
{
$var = $set_checked;
}
}
function updateNewsletter($frm)
{
check_checked($frm['n_deleted']);
$sql = db_query("UPDATE ". DB_PREFIX ."newsletter
SET n_title = '$frm[n_title]',
n_description = '$frm[n_description]',
n_deleted = '$frm[n_deleted]',
n_modified = NOW()
WHERE n_id = '$frm[n_id]'
" );
}
EDIT:
Forgot to mention that i don't use ENUM's but bitfield (TINYINT or INT) for 0 or 1. Might make things easier for you.Last edited by frezno; Sep 18, 2003 at 02:47.
We are the Borg. Resistance is futile. Prepare to be assimilated.
I'm Pentium of Borg.Division is futile.Prepare to be approximated.
Bookmarks