...

View Full Version : how to update users table depending on checkbox



Dan13071992
01-17-2012, 01:37 AM
hi guys,

ive got a bit of code which is ment to update a check box, if $fetch2->musichear == "0" the checkbox should be unchecked, elseif == "1" it should be checked, now ive sorted that bit out, its the updating into the database on wether its checked or not thats giving me the problem. my code is below:



if (($_POST['updateOptions']) && ($_POST['hear_music'])){
$hearmusic=strip_tags($_POST['hear_music']);

mysql_query("UPDATE users SET musichear='".dbSafe($hearmusic)."' WHERE username='$fetch->username'");
$message= "Your settings have been updated";
}




<input type=checkbox <?php echo "$checked"; ?> name="hear_music">


cheers.

Dan

mlseim
01-17-2012, 02:49 AM
Checkboxes with PHP act a bit different than other form tags.

You'll only get a value in your PHP if the checkbox is checked.
And you don't really check for a value, you check for "isset".
If it's set, you read it's value.

You have to decide if you want to fill the "musichear" column with YES, NO, 1, 0, or nothing or something else.

You can define a value if you want in here, or wait until you check it with the PHP ...
<input type="checkbox" name="hear_music" value="YES" <?php echo "$checked"; ?> >

Remember though, if someone DOES NOT check the checkbox, your PHP script
will not even know it's there ... it doesn't exist unless it's checked.

So, it would be something like this ...


<?php
$hearmusic="NO";
if(isset($_POST['hear_music'])){
$hearmusic="YES";
}
if ($_POST['updateOptions']){
mysql_query("UPDATE users SET musichear='".dbSafe($hearmusic)."' WHERE username='$fetch->username'");
$message= "Your settings have been updated";
}

Dan13071992
01-17-2012, 12:45 PM
Remember though, if someone DOES NOT check the checkbox, your PHP script
will not even know it's there ... it doesn't exist unless it's checked.


Thanks for all of your reply, ive seen sites before that update these check boxes to yes and no at the same time :S.

I understand what your saying, but if i have it checked, then when i dont want to hear music, i uncheck it, is there not a way to update the DB because its not checked?

like for example your saying that php wont detect it if its not checked, so is there a way to use that to the advantage, if (!isset(checkbox)) then update users table?

would that work?

BluePanther
01-17-2012, 12:57 PM
if (!isset(checkbox)) then update users table?

would that work?

Yes, that would work. Although, I would check for isset(), then have an else for that condition.

mlseim
01-17-2012, 01:01 PM
You can do it either way.
I'm just seeing that you actually are putting a checkbox value into your table.
So, that tells me you wish to update it whether its checked or not, but put a
different value in the column depending on the state.

If you only update the table if the checkbox is checked, what is the purpose of
the column you call "musichear" ?

Dan13071992
01-17-2012, 01:09 PM
I just realised that this code you gave me mlseim is the right code:



$hearmusic="NO";
if(isset($_POST['hear_music'])){
$hearmusic="YES";
}
if ($_POST['updateOptions']){
mysql_query("UPDATE users SET musichear='".dbSafe($hearmusic)."' WHERE username='$fetch->username'");
$message= "Your settings have been updated";
}


basically if its checked, users can hear music in other peoples profiles, if not checked they wont hear the music, that code you provide above does the job :) thanks alot :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum