duanche4578 2018-01-04 12:41
浏览 61

使用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.

  • 写回答

2条回答 默认 最新

  • dsf5632 2018-01-04 13:10
    关注

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来