Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    how to update users table depending on checkbox

    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:

    PHP Code:
    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";

    Code:
    <input type=checkbox <?php echo "$checked"; ?> name="hear_music">
    cheers.

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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 Code:
    <?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"
    }

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by mlseim View Post
    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?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by Dan13071992 View Post
    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.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    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" ?

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    I just realised that this code you gave me mlseim is the right code:

    PHP 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
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •