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:
What am I missing here? Thanks for your time and feedback!