dongmeng1868 2017-05-05 06: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 06: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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部