EDIT: Edit of the original code: (I changed the if to "if($_POST['Break'] != "")" to test it and it doesnt work, neither do any of the other varients that i've tried.
if($_SERVER['REQUEST_METHOD'] != 'POST')
{
echo '<form method="post" action="">
Category name: <input type="text" name="cat_name" />
Category description: <textarea name="cat_description" /></textarea>
<input type="checkbox" value="Break">Is Table Break?<br>
<input type="submit" value="Add category" />
</form>';
}
$sql= 'INSERT INTO categories(cat_name, cat_description, isheader) VALUES (:cat_name, :cat_description, :isheader)';
$stmt = $DBH->prepare($sql);
if($_POST['Break'] != ""){
$isbreak = true;
}
else{
$isbreak = false;
}
$stmt->bindParam(':cat_name', $_POST['cat_name']);
$stmt->bindParam(':cat_description', $_POST['cat_description']);
$stmt->bindParam(':isheader', $isbreak);
try{
$stmt->execute();
header('Location: /testpage.php');
}
catch(PDOexception $e){
$e->getMessage();
}
The above code should insert into my database with Column "Break" being set to "True"(or 1) when a checkbox is checked. It doesnt. I've tried the following if statements and none fixed it:
if(isset($_POST['Break']) == 1)
if(($_POST['Break']) == "Break") - ("Break" being the name of my checkbox.
if(($_POST['Break']) === "Break")
if(($_POST['Break']) == 'Break')
Now i know this code SHOULD work because before i converted to PDO php it was working. Heres what my previous code looked like. This was 100% working how i wanted it to:
if(isset($_POST['Break']) == 1){
$isbreak = true;
}
else{
$isbreak = false;
}
$sql = "INSERT INTO categories(cat_name, cat_description, isheader)
VALUES('" . mysql_real_escape_string($_POST['cat_name']) . "',
'" . mysql_real_escape_string($_POST['cat_description']) . "', ". $isbreak.")";
$result = mysql_query($sql);
if(!$result)
{
echo 'Error' . mysql_error();
}
else
{
header('Location: /testpage.php');
};
I know some of the $_POST data works because my database is filled with the correct "cat_name" and "cat_description" with the PDO code. I've had this problem for EVERY page on my site converting it. I've managed to find dumb little work around specific to each page, but i cant figure this one out. I'd rather just know why this is acting the way it is.
What's more is that when i do print_r($_POST) and my check box is checked it returns the value "Break". I dont understand it.