• This testing page selects a single record from a custom table based on an ID. The data is text and checkboxes. The text data displays correctly and the echo sql statement reports the checkbox data returns TRUE, FALSE, 1 or 0. However none of the checkboxes show a check. ??
    Code below displays a value for the OtherWriteIn textfield
    echo "<tr><th>List Your Organizations</th><td><input type=text name=OtherWriteIn value='".$row->OtherWriteIn."'></td></tr>";
    Code below does not display a check for the checkbox field
    echo "<tr><th>All Those Books</th><td><input type=checkbox name=AllThoseBooks value='".$row->AllThoseBooks."'></td></tr>";

    The page I need help with: [log in to see the link]

Viewing 9 replies - 31 through 39 (of 39 total)
  • Thread Starter hsysgrp

    (@hsysgrp)

    I have added that tip to my notes. Next task is to insert the changes. Is inserting the values as tricky as displaying them? Can I just
    $sql = “UPDATE AAUW_Members SET FirstName = ‘$_POST[FirstName]’,LastName = ‘$_POST[LastName]’,Address1 = ‘$_POST[Address1]’,City = ‘$_POST[City]’,State =
    ‘$_POST[State]’, Zip = ‘$_POST[Zip]’,HomePhone = ‘$_POST[HomePhone]’,CellPhone = ‘$_POST[CellPhone]’,Email = ‘$_POST[Email]’,College1 = ‘$_POST[College1]’,College2 = ‘$_POST[College2]’,College3=’$_POST[College3]’,Birth_Day = ‘$_POST[Birth_Day]’,Birth_Month = ‘$_POST[Birth_Month]’,Mem_Type = ‘$_POST[Mem_Type]’,Honorary = ‘$_POST[Honorary]’,Joined_Local = ‘$_POST[Joined_Local]’, Joined_Natl = ‘$_POST[Joined_Natl]’,Mailings = ‘$_POST[Mailings]’,Positions_Held = ‘$_POST[Positions_Held]’,Notes = ‘$_POST[Notes]’,Referred = ‘$_POST[Referred]’, Retired = ‘$_POST[Retired]’,Employer = ‘$_POST[Employer]’,Occupation = ‘$_POST[Occupation]’,Positions = ‘$_POST[Positions]’,
    AllThoseBooks = ‘$_POST[AllThoseBooks]’,
    ArtOnTheGo = ‘$_POST[ArtOnTheGo]’,
    AdventuresEnSoleil = ‘$_POST[AdventuresEnSoleil]’,
    Bridge = ‘$_POST[Bridge]’,
    Canasta = $_POST[Canasta],
    ContemporaryLit = ‘$_POST[ContemporaryLit]’,
    Cuisine = ‘$_POST[Cuisine]’,
    DaytimeLit = ‘$_POST[DaytimeLit]’,
    Gourmet = ‘$_POST[Gourmet]’,
    MahJongg = ‘$_POST[MahJongg]’,
    Manderley = ‘$_POST[Manderley]’,
    Movies = ‘$_POST[Movies]’,
    PedalPushers = ‘$_POST[PedalPushers]’,
    PinsAndNeedles = ‘$_POST[PinsAndNeedles]’,
    PoetryAndPlays = ‘$_POST[PoetryAndPlays]’,
    ReelyTogether = ‘$_POST[ReelyTogether]’,
    Trekkers = ‘$_POST[Trekkers]’,
    WordGames = ‘$_POST[WordGames]’,
    WorldTravelers = ‘$_POST[WorldTravelers]’
    WHERE ID = ‘$_POST[ID]’; “;

    In theory, yes this will work.
    But …. It’s just very old school.
    It’s not scalable, it’s not in the WordPress framework, it’s not sanitized, etc etc.
    I assume you are only using this as an intranet? Or that the live version is not available over port 80?

    How many records do you have?
    And why are you using this method?
    At bare minimum, you should using prepare and insert using the wpdb object classes:
    https://developer.www.ads-software.com/reference/classes/wpdb/

    Again, none of this is really related to WordPress.

    Thread Starter hsysgrp

    (@hsysgrp)

    I have ~400 records, I know it’s not sanitized yet, when finished it will be only maintained by the membership committee and the info distributed, so maybe four users. I have not yet found advice on best practice for managing data in tables with umptyump fields, all the examples have maybe six columns. Should I be storing the checkbox data as 1/0 instead of yes/no or True/False…was using checkboxes a bad idea, who knew I needed a foreach to get a click.
    I am using this method because I am teaching myself php and I didn’t want to use plug-ins.

    You should be storing Boolean datatypes as 1 or 0.

    > I have not yet found advice on best practice for managing data in tables

    Use a Membership plugin. And add your custom fields to the profile :
    https://en-ca.www.ads-software.com/plugins/ultimate-member/

    Thread Starter hsysgrp

    (@hsysgrp)

    The form-handler displays the $sql update as
    Connected Successfully5293479UPDATE AAUW_Members SET FirstName = ‘Claudette’,LastName = ‘Hennessy’,Address1 = ’20 Dutchess Landing Road, Apt B211′,City = ‘Poughkeepsie’,State = ‘NY’, Zip = ‘12601’,HomePhone = ‘n/a’,……., AllThoseBooks = ‘AllThoseBooks’, ‘WorldTravelers’ WHERE ID = ‘5293479’ Not updated
    So when the input field is text, the $_POST[FirstName] correctly attempts to input a string value, but when the input field is a checkbox, the AllThoseBooks = ‘$_POST[AllThoseBooks]’, is attempting to input AllThoseBooks rather than the boolean 1, hence the input statement fails?

    The sql statement is `$sql = “UPDATE AAUW_Members SET FirstName = ‘$_POST[FirstName]’,LastName = ‘$_POST[LastName]’,Address1 = ‘$_POST[Address1]’,City = ‘$_POST[City]’,State =
    ‘$_POST[State]’, Zip = ‘$_POST[Zip]’,HomePhone = ‘$_POST[HomePhone]’,CellPhone = ‘$_POST[CellPhone]’,Email = ‘$_POST[Email]’,College1 = ‘$_POST[College1]’,College2 = ‘$_POST[College2]’,College3=’$_POST[College3]’,Birth_Day = ‘$_POST[Birth_Day]’,Birth_Month = ‘$_POST[Birth_Month]’,Mem_Type = ‘$_POST[Mem_Type]’,Honorary = ‘$_POST[Honorary]’,Joined_Local = ‘$_POST[Joined_Local]’, Joined_Natl = ‘$_POST[Joined_Natl]’,Mailings = ‘$_POST[Mailings]’,Positions_Held = ‘$_POST[Positions_Held]’,Notes = ‘$_POST[Notes]’,Referred = ‘$_POST[Referred]’, Retired = ‘$_POST[Retired]’,Employer = ‘$_POST[Employer]’,Occupation = ‘$_POST[Occupation]’,Positions = ‘$_POST[Positions]’,
    AllThoseBooks = ‘$_POST[AllThoseBooks]’,
    ArtOnTheGo = ‘$_POST[ArtOnTheGo]’,`

    Then the object is wrong.
    You’ll need to post all your UPDATE file back to pastebin.

    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you.
    https://pastebin.com/vbKLw2Vj is the link to the updatecheckbox paste

    Thread Starter hsysgrp

    (@hsysgrp)

    Well, this is interesting. I changed TRUE/FALSE in the source table to 1,0. The checkbox fields are set to varchar(5). print_r for the select qry displays in part
    [Positions] => Database Manager [AllThoseBooks] => AllTh [ArtOnTheGo] => ArtOn [AdventuresEnSoleil] => Adven [Bridge] => [Canasta] => 0 [ContemporaryLit] => Conte [Cuisine] => Cuisin [DaytimeLit] => [Gourmet] => [MahJongg] => [Manderley] => [Movies] => [PedalPushers] => [PinsAndNeedles] => [PoetryAndPlays] => Poetr [ReelyTogether] => [Trekkers] => [WordGames] => [WorldTravelers] => World [AccountingBookkeeping] => 0 [DataEntry] => 1 …
    So the update is actually inserting the first 5 characters of the field name instead of 1 if it is checked. which means $checked = ($row->$cb == ‘1’) ? ‘checked’: ”; is setting the value to the fieldname instead of the field value.

    foreach( $checkboxes as $cb)	{
    			$checked = ($row->$cb == '1') ? 'checked': '';
    				
    		echo '<tr><th>'. $cb .'</th><td><input type="checkbox" name="'. $cb .'" value="' . $cb .'" ' . $checked . ' /></td></tr>';

    You two are still talking about this?
    You should look at the code I gave. You have to have the field name somewhere, or you end up with a bunch of 0 and 1s with no idea what they mean. It makes better sense to use the field name if it is checked and nothing if it is not. That is what the browser does for you anyway. I also gave you a link to the WP function checked() which sets the attribute for you…

Viewing 9 replies - 31 through 39 (of 39 total)
  • The topic ‘Checkbox not checked’ is closed to new replies.