read bit value into checkbox in PHP
Results 1 to 7 of 7

Thread: read bit value into checkbox in PHP

  1. #1
    Senior Member
    Join Date
    Oct 2011
    Posts
    267

    read bit value into checkbox in PHP

    Hi,

    I am having difficulties reading the bit value from MySQL and showing it in a checkbox in PHP.

    The value is not showing and it's always unchecked!

    This is what I am using:

    PHP Code:
    $mysql_command "CALL sp_get_member_personal_information(:param_member_guid)";

    $mysql_query $mysql_connection->prepare($mysql_command);
    $mysql_query->bindParam(':param_member_guid'$_SESSION["member_guid"], PDO::PARAM_STR);

    $mysql_query->execute();

    $mysql_row_count $mysql_query->rowCount();

    if (
    $mysql_row_count <= 0) { exit(header("Location: home")); }

    while (
    $mysql_row $mysql_query->fetch())
    {
        
    $is_private = (bool)$mysql_row["is_private"];
        
    $hide_contact_details = (bool)$mysql_row["hide_contact_details"];

    and then:

    PHP Code:
    <input type="checkbox" id="chkPrivateProfile" name="chkPrivateProfile" value="<?php echo $is_private?>"> My Profile is Private<br><br>
    <input type="checkbox" id="chkHideContactDetails" name="chkHideContactDetails" value="<?php echo $hide_contact_details?>"> Hide my contact details<br><br>

    I have no problem reading a varchar value but my problem is juts with the bit as it's my first time trying checkbox with php.

    Please help...

    Thanks,
    Jassim
    Last edited by jrahma; 10-25-2013 at 08:54 AM.

  2. #2
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by jrahma View Post
    I am having difficulties reading the bit value from MySQL and showing it in a checkbox in PHP.

    The value is not showing ...
    Have you tried var_dumping out your $is_private and $hide_contact_details variables to make sure they contain the values you think they do? Keep in mind that, since you're casting to boolean, you will only see anything printed to the page if it is truthy (print false; prints an empty string; print true; prints 1).


    Quote Originally Posted by jrahma View Post
    and it's always unchecked!
    That's because you're not checking them.
    HTML Code:
    <input type=checkbox value=whatever checked>
    <!-- or, of course, by clicking on them before submitting the form -->
    However, I would think that this approach would be confusing and unreliable - you're dynamically determining the value of the input, but the label is always the same. How will the user know what option they are choosing?
    Last edited by traq; 10-25-2013 at 10:37 AM.

  3. #3
    Senior Member
    Join Date
    Oct 2011
    Posts
    267
    var_dumping is dumping the correct value (true and false) but when I changed the html to the following code I get it checked ALWAYS even when the value was var_dumped as false:

    PHP Code:
    <input type="checkbox" id="chkPrivateProfile" name="chkPrivateProfile" value="<?php echo $is_private?><?php if ((bool)$is_private true) { ?>checked="checked"<?php ?>> My Profile is Private<br><br>
    <input type="checkbox" id="chkHideContactDetails" name="chkHideContactDetails" value="<?php echo $hide_contact_details?><?php if ((bool)$hide_contact_details true) { ?>checked="checked"<?php ?>> Hide my contact details<br><br>

  4. #4
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Alright; to clarify: the value of a checkbox has nothing to do with whether or not it is "checked." It's just the value that is assigned to the form field if the box is checked.

    If you're trying to check the box depending on whether the value from the DB is true or false, you'd need to do the exact opposite: the value should always be the same, and the checked attribute should be dynamically determined:
    PHP Code:
    <?php

    //  . . . 

    $check_is_private = ($mysql_row["is_private"])? 'checked''';

    //  . . .

    ?>
    <input type=checkbox name=chkPrivateProfile value=1 <?= $check_is_private ?> >
    = = = = = edit = = = = =

    I just saw what you meant by "checked ALLWAYS":
    PHP Code:
    ((bool)$is_private true
    You're making an assignment there, not a comparison. Use triple equal ( === ) for equality (double equal ( == ) for loose equality; equal ( = ) for assignment).
    Last edited by traq; 10-25-2013 at 12:57 PM.

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,865
    Just as a matter of code design:
    PHP Code:
    if ($mysql_row_count <= 0) { exit(header("Location: home")); }

    while (
    $mysql_row $mysql_query->fetch())
    {
        
    $is_private = (bool)$mysql_row["is_private"];
        
    $hide_contact_details = (bool)$mysql_row["hide_contact_details"];

    Is there (a) much point in testing if fewer than 0 rows are returned, (b) writing an entire loop if you're going to ignore the results of all but one row?
    Last edited by Weedpacket; 10-26-2013 at 10:48 AM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  6. #6
    Senior Member
    Join Date
    Oct 2011
    Posts
    267
    Thanks. My problem is solved using traq's code.

  7. #7
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    glad to hear it. But Weedpacket's question is still valid - are you getting more than one row? would it cause problems if you did?

    If you need + expect only one row, then looping is pointless. Additionally, you should test for what you need (1 row) rather than not-exactly-what-you-need (1 or more rows). Something like this would be a better pattern:
    PHP Code:
    <?php

    //  . . .

    $stmt->execute()

    if( 
    $stmt->rowCount() === ){
        
    $result $stmt->fetch();
        
    /*  do stuff  */
    }
    else{
        
    /*  problem!  */
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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