Trouble with inserting Checkbox values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raamay
    New Member
    • Feb 2007
    • 107

    Trouble with inserting Checkbox values

    I have six checkboxes as shown below:
    Code:
    <table>
    <tr>
    <td><input name="spec1" type="checkbox" value="0" tabindex="11" /><label id="label">Bridge Construction</label></td>
    </tr>
    <tr>
    <td><input name="spec2" type="checkbox" value="1" tabindex="12" /><label id="label">Building Construction</label></td>
    </tr>
    <tr>
    <td><input name="spec3" type="checkbox" value="2" tabindex="13" /><label id="label">Dam Construction</label></td>
    </tr>
    <tr>
    <td><input name="spec4" type="checkbox" value="3" tabindex="14" /><label id="label">Power & Telecommunication Works</label></td>
    </tr>
    <tr>
    <td><input name="spec5" type="checkbox" value="4" tabindex="15" /><label id="label">Road Construction</label></td>
    </tr>
    <tr>
    <td><input name="spec6" type="checkbox" value="5" tabindex="16" /><label id="label">Others</label></td>
    </tr>
    </table>
    And I have been trying to insert the checkbox values to my database in the following way.
    Code:
    if($_POST['spec1']==0){ $spec1="1";} else { $spec1="0"; }
    if($_POST['spec2']==1){ $spec2="1";} else { $spec2="0"; }
    if($_POST['spec3']==2){ $spec3="1";} else { $spec3="0"; }
    if($_POST['spec4']==3){ $spec4="1";} else { $spec4="0"; }
    if($_POST['spec5']==4){ $spec5="1";} else { $spec5="0"; }
    if($_POST['spec6']==5){ $spec6="1";} else { $spec6="0"; }
    Everything goes fine except that even if i dont check the first checkbox, the column corresponding to this checkbox in the database gets a value of 1. To be clear, if i check spec4 and spec5 the result stored in the database is fine as the corresponding column for this two checkbox is valued with 1. But along with it the value for spec1 is also added although i didn't check the first checkbox.

    Any help in this situation could be a great help! Kindly correct my code if necessary or kindly help me how to insert multiple checkbox values in different fields of a table.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    this is probably because NULL is equivalent to 0 in a simple boolean test. try following (value and type comparison):
    Code:
    $spec1 = ($_POST['spec1'] === 0) ? "1" : "0";

    Comment

    • raamay
      New Member
      • Feb 2007
      • 107

      #3
      Still it doesnt solve the problem. I have changed it in the following manner:
      Code:
      $spec1 = ($_POST['spec1'] === 0) ? "1" : "0";
      $spec2 = ($_POST['spec2'] === 1) ? "1" : "0";
      $spec3 = ($_POST['spec3'] === 2) ? "1" : "0";
      $spec4 = ($_POST['spec4'] === 3) ? "1" : "0";
      $spec5 = ($_POST['spec5'] === 4) ? "1" : "0";
      $spec6 = ($_POST['spec6'] === 5) ? "1" : "0";
      It gives me database error. I mean in my form if there is any error with the insert operation it returns me back the form that i submitted. That is what happening at present.

      Plus if i check the first checkbox along with some others, the values are properly stored. Which means i have to check the first checkbox by any means. This output is available if i change it in the following way:
      Code:
      $spec1 = ($_POST['spec1'] === "0") ? 1 : 0;
      $spec2 = ($_POST['spec2'] === "1") ? 1 : 0;
      $spec3 = ($_POST['spec3'] === "2") ? 1 : 0;
      $spec4 = ($_POST['spec4'] === "3") ? 1 : 0;
      $spec5 = ($_POST['spec5'] === "4") ? 1 : 0;
      $spec6 = ($_POST['spec6'] === "5") ? 1 : 0;

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what exactly is the database error?

        Comment

        • raamay
          New Member
          • Feb 2007
          • 107

          #5
          Database error is something like if some text value is inserted in a int field. There can be many of them. And by the way my db table has int(1) as the data type to store the value of checkboxes.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            then try to get the sql error message. like (depending on your code there may be different approaches to get the error message)
            Code:
            mysql_query($sql) or die(mysql_error());
            
            // in case you use exceptions
            try {
            // db code
            }
            catch (Exception $e)
            {
                echo $e->getMessage();
            }
            otherwise guessing what the error might be is not very effective.

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              You're trying to insert a string type into an int type column. Correct this and you'll be OK.

              Comment

              • devsusen
                New Member
                • Feb 2007
                • 136

                #8
                Your code should be :
                Code:
                $spec1 = (isset($_POST['spec1'])) ? 1 : 0; 
                $spec2 = (isset($_POST['spec2'])) ? 1 : 0;  
                $spec3 = (isset($_POST['spec3'])) ? 1 : 0;  
                $spec4 = (isset($_POST['spec4'])) ? 1 : 0;  
                $spec5 = (isset($_POST['spec5'])) ? 1 : 0;  
                $spec6 = (isset($_POST['spec6'])) ? 1 : 0;
                Last edited by devsusen; Feb 27 '09, 12:46 PM. Reason: editing the error mentioned by Dormilich

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  Originally posted by devsusen
                  Your code should be :
                  note: line #2 is missing a closing parenthesis

                  Comment

                  • raamay
                    New Member
                    • Feb 2007
                    • 107

                    #10
                    sorry guys, there was no mistake in the code. it was a silly mistake of my own which i have solved. Anyway thankyou and sorry for troubling you all.

                    Comment

                    Working...