Luke Litherland
Luke Litherland

Reputation: 227

Insert a checkbox selection value into database as Bit field using PHP

I've been through a few similar questions for this, however, with my basic knowledge I haven't been able to find an answer that I can tie directly to what I'm trying to achieve.

For instance, I have found answers regarding arrays to store multiple checkbox values. However, I'm not sure if this is what I need as each of the 3 checkboxes i have are for individual columns in the database.

This is my HTML that i have currently for checkboxes etc:

<form action="addNewClient.php" method="POST">
          <div class="modal-body">
            <p>Please enter the name of the client you wish to create.</p>
            <textarea id="addSQLNoteName" placeholder="Enter client name..." name="title" maxlength="25"></textarea>
            <p>Teams Packge:</p>
            <select name="package">
              <option value="SBE">SBE</option>
              <option value="Enterprise">Enterprise</option>
            </select>
            <br />
            <input type="checkbox" name="portal" value="1"> Premium Portal
            <br />
            <input type="checkbox" name="replicated" value="1"> Replicated
            <br />
            <input type="checkbox" name="client" value="1"> IT Client
            <br />
            <textarea id="addSQLNoteName" placeholder="Important client info..." name="info" maxlength="25"></textarea>
          </div>
          <div class="modal-footer footer-sqlnotes">
            <button type="submit" class="btn btn-success">Add Client</button>
          </div>
      </form>

This is my current PHP code and ive made a few comments just showing which ones are the checkboxes:

<?php include 'connectionDetails.php'; ?>

<?php

session_start();

if (isset($_POST['title'], $_POST['package'], $_POST['portal'], $_POST['replicated'], $_POST['client'], $_POST['info']))
{
    $title = $_POST['title'];
    $package = $_POST['package']; 
    $portal = $_POST['portal'];         //Checkbox
    $replicated = $_POST['replicated']; //Checkbox
    $client = $_POST['client'];         //Checkbox
    $info = $_POST['info'];

    $stmt = "INSERT INTO Clients (Client, TeamsPackage, Rating, Pos, Neg, PremiumPortal, Replicated, ITClient, ClientInfo) VALUES (?, ?, 0, 0, 0, ?, ?, ?, ?)";
    $params = array($title, $package, $portal, $replicated, $client, $info);

    $stmt = sqlsrv_query($conn, $stmt, $params);

    if ($stmt === false) 
    {
        die( print_r(sqlsrv_errors(), true));
    }

    header('location: clients.php');
}

else
{
    echo "No Data";

}


?>

And so in my database, I have three bit field columns, if checked when submitted it will enter as a 1 otherwise it will enter as a 0.

Like I said, I apologise if another question does answer mine, I just couldn't seem to implement it over to what i am trying to achieve.

At the moment, if all 3 boxes are checked then it will add the client, otherwise it fails the isset() which is what I'm trying to get around at the moment.

Upvotes: 1

Views: 1427

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Get rid of this entire statement:

if (isset($_POST['title'], $_POST['package'], $_POST['portal'], $_POST['replicated'], $_POST['client'], $_POST['info']))

Name your submit button:

<button type="submit" class="btn btn-success" name="submit">Add Client</button>

Then change your conditional to:

Side note: See the Edit: below.

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

    $portal = (isset($_POST['portal'])) ? '0' : $_POST['portal'];
    $replicated = (isset($_POST['replicated'])) ? '0' : $_POST['replicated'];
    $client = (isset($_POST['client'])) ? '0' : $_POST['client'];

    // add your other form elements 

    // Perform your query here
}

Ternary operators are IMHO, what should be used here.

Using a default value of 0.

You can add the other conditionals from your original post for the other form elements.


Edit:

I made a mistake with the isset()'s for the ternaries. Those should have read as !isset() checking if they are "not" set.

So, I rewrote it as the following:

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

     $portal = !isset($_POST['portal']) ? '0' : $_POST['portal'];
     $replicated = !isset($_POST['replicated']) ? '0' : $_POST['replicated'];
     $client = !isset($_POST['client']) ? '0' : $_POST['client'];

    // add your other form elements 

    // Perform your query here
}

Upvotes: 2

Luke Litherland
Luke Litherland

Reputation: 227

Credit to @FunkFortyNiner who left me with this solution in a discussion.

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

$portal = $_POST['portal']; 

}else{ 

$portal = 0; 

}

His original answer for some reason wasn't entering the correct values, however this that he suggested has worked.

Rather than using a ternary operator, just doing an isset statement on each of the check boxes and if not checked setting it to 0.

Upvotes: 1

Related Questions