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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题