dongmeng1868 2017-05-05 14:25
浏览 74
已采纳

HTML表单在数据库中发布除TinyINT之外的正确值

I'm kinda new to php and I'm having a bit of a struggle getting the values from an HTML form into my Database. The column I'm having a problem with is a TINYINT. In my understanding this can have 2 values, 1 or 0 but I'm not getting this result in my database. The rest of the form seems to work accordingly. Here is an example of my code and logic;

The HTML form looks like this:

    <form action="" method="post" id="createBoard" enctype="multipart/form-data">
        <label for="boardTitle">Name</label>
        <input type="text" name="boardTitle" id="boardTitle">

        <label for="privateSwitch">Private?</label>
        <input id="privateSwitch" name="status" type="checkbox" checked>
        <br>
        <input type="submit" value="Submit" />
    </form>

The PHP logic before the html is:

if (!empty($_POST)) {
    try {
        // create prepared statement
        $newBoard = new Board();
        $newBoard->setBoardName($_POST["boardTitle"]);
        if (empty($_POST['status'])){
            $privateSwitch = 0;
        } else {
            $privateSwitch = 1;
        }
        $newBoard->setPrivateSwitch($privateSwitch);
        if ($newBoard->create()){
            $feedback = "Board saved";
            header("Location: user_uploads.php?success=true");
        }

    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

And the class included looks as following:

Setter & Getter:

public function setPrivateSwitch($privateSwitch)
{
  $this->privateSwitch = $privateSwitch;
}

public function getPrivateSwitch()
{
    return $this->privateSwitch;
}

The fuction for creating a "board":

public function create()
{
    $conn = Db::getInstance();
    $stmt = $conn->prepare("INSERT INTO board (boardTitle, userID, private) VALUES (:boardTitle, :userID, :private)");
    $stmt->bindValue(":boardTitle", $this->boardTitle);
    $stmt->bindValue(":userID", $_SESSION["id"]);
    $stmt->bindValue(":private", $this->private);
    return $stmt->execute();
}

The Board is being created in the database but the private row is still getting NULL values. It looks like this:

Screenshot of the Database Table board

Structure of the Database

What am I missing here? Thanks for your time and feedback!

  • 写回答

1条回答 默认 最新

  • dsce23640 2017-05-05 14:34
    关注

    The setter function seems to be the issue. Try changing setter function to:

    public function setPrivateSwitch($privateSwitch)
    {
      $this->private = $privateSwitch;
    }
    

    and getter to:

    public function getPrivateSwitch()
    {
      return $this->private;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来