1

I am trying to get values from a form into my database as boolean type.

Basically, each question has many checkboxes, and if a user checks the box, i want it to put in 1 for true and whichever ones are not checked to insert 0.

I have attempted to get them in, however no luck.

Please see additional code/snippets below.

Can someone please help?

  <h1>Question 1</h1><br>
   </br>
 <input type="checkbox" name="1" value="1">Item1
 <input type="checkbox" name="2" value="2">Item2
<input type="submit" name="submit" value="save" class="button-form">

PHP CODE:

  if(isset($_POST['submit'])){

 try{

    $Query = $db->prepare('INSERT INTO Results ( 1, 2 ) VALUES (:1, :2)');
        $Query->execute();
3
  • If you want a value of 1, why are you setting the values the same as the names? And note that unchecked checkboxes are not sent to the server. Commented Mar 14, 2018 at 11:14
  • B1 is the name defined in the database, i want it to give value of 1 IF CHECKED? Commented Mar 14, 2018 at 11:14
  • You might want to look into using array_key_exists(). Commented Mar 14, 2018 at 11:52

2 Answers 2

2

When a checkbox isn't checked, it's not sent. That means it is not set to zero if unchecked.

What does that mean? It means you need to check if it's sent or not. If yes, it's 1, if not - it's 0.

The code you're after is the following:

$stmt->execute(array(
            ':UserID' =>$_POST['UserID'],
            ':B1' => isset($_POST['B1']) ? 1 : 0,
            ':B2' => isset($_POST['B2']) ? 1 : 0, 
            ':B2' => isset($_POST['B3']) ? 1 : 0,
            ':B2' => isset($_POST['B4']) ? 1 : 0,
            ':B2' => isset($_POST['B5']) ? 1 : 0,
            ':B2' => isset($_POST['B6']) ? 1 : 0
            ));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that. I have put that in, but it is carrying 0 to all answers to the database still?
Your query inserts only 3 values. INSERT INTO Results ($UserID, B1, B2 ) . I can't see what the http headers are so it's a bit impossible to aid further without additional input from you..
0

If a checkbox isn't checked (see "checked"-Attribute of input type checkbox), nothing is sent to the server. $_POST['B1'] gets the value of the element with NAME 'B1' if it is sent to the server.

So

$b1 = (isset($_POST['B1'])) ? 1 : 0;

should do the trick. $b1 will be 0 if the checkbox is unchecked and 1 if it is checked.

Option 1 The SQL looks like you are trying to put something like 01100 into the database field 'B2'. Usually you would store the result of every checkbox in a single database column. But if you really want to concat them, your code should look like this:

$stmt = $conn->prepare('INSERT INTO Results ($UserID, B1, B2 ) VALUES (:UserID, :B1, :B2, :B3, :B4, :B5, :B6);');
    $stmt->execute(array(
        ':UserID' =>$_POST['UserID'],
        ':B1' =>$_POST['B1'],
        ':B2' =>(isset($_POST['B2'])) ? 1 : 0 + (isset($_POST['B3'])) ? 1 : 0 + (isset($_POST['B4'])) ? 1 : 0 + (isset($_POST['B5'])) ? 1 : 0 + (isset($_POST['B6'])) ? 1 : 0
        ));

Option 2 If you have one database field for every B1...B6-value then you've forgotten to take these columns into your SQL-INSERT-Statement:

INSERT INTO Results ($UserID, B1, B2, B3, B4, B5, B6) [...]

And your stmt execute is wrong and should be:

$stmt->execute(array(
        ':UserID' =>$_POST['UserID'],
        ':B1' => isset($_POST['B1']) ? 1 : 0,
        ':B2' => isset($_POST['B2']) ? 1 : 0, 
        ':B3' => isset($_POST['B3']) ? 1 : 0,
        ':B4' => isset($_POST['B4']) ? 1 : 0,
        ':B5' => isset($_POST['B5']) ? 1 : 0,
        ':B6' => isset($_POST['B6']) ? 1 : 0
        ));

4 Comments

Do it like N.B. mentioned above: ':B1' => isset($_POST['B1']) ? 1 : 0, ... But take care - your SQL isn't complete. You're inserting ($UserID, B1, B2 ) only. You should take the other columns in addition.
Is there an easier way with: ':B1' =>$_POST['B1'], as I have perhaps 30 checkboxes, or is that the way it has to be done?
It would be easier to answer if you could provide more information of what are you doing - Option 1 (storing 0/1 of ALL questions in ONE database field) or Option 2 (storing EVERY single 0/1 in a SINGLE database field).
Option 2 - Storing EVERY single 0/1 in a single database field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.