true = 1, false != 0, DB refusal
Hey so, basically it's like this:
So I got a checkbox that I have to put in the DB for priority
I already concluded that it just sends either "on" or nothing depending on when its selected or not, it does in fact enter the correct case as well
If I set it to true, it returns as 1, all fine and dandy there
however, in the else statement, if I were to set it to false, it basically sets it to an empty string returning in an error because the DB is expecting a bool (1 or 0, 0 being default)
Why is this?
Is it fixable or should I just stay with the else just setting it to 0?


13 Replies
quickly the error for when I'm using false isntead of 0

it looks like your column is defined as an INT type of some sort (I personally use(d) TINYINT(1) for boolean columns a lot). PHP translates
true
into 1 if it needs to cast it to a string or number, and casts to an empty string for false
.
I'd use this:
instead of the if statement on the leftI see, while you're basically using the same function but with less lines (one-liner if/else), you're saying to abandon trying to use false and just work with 0 directly instead
as yes the db is using tinyint(1)
either that or change the DB to use a boolean type, that might work too
well, im pretty sure the db actually changed BOOL to tinyint itself
yeah, it depends on the DB if it's supported
you're not necessarily abandoning working with boolean values, you're translating the data that comes from the browser into the data that needs to be stored in the database, which is really a good chink of what backend programming is
bc im pretty sure originalmy I said its of type bool and the db changed it to tinyint(1)
its mysql
I mean tbf im fine with using 1 and 0 directly, its all the same to me
I was more wondering why true did render what it was supposed to while false didn't
perhaps if I predefine that prioriteit will only accept ints it would translate the false correctly
but thats too much effort might as well "abandon" true/false and instead use 1/0 its the same effect in all honesty just needs more thinking for anyone that reads it to understand which isnt an issue
the original isnt a bool either, php renders a checkbox as "on" or nothing if its sent through a POST form
so I dont need to work w true false in general, 1 and 0 will do fine
as its reading value in post and not checked
if you can define types for the prepared statement, it might work. But it's just a part of PHP works, that
false
casts to empty string
.
That's technically not PHP, but HTML. The way the spec defines forms should work is that unchecked checkboxes are not part of the POST payload when it's submitted, and checked ones send the contents of the value
property with the key of the name
propertyif it is mysql or mariadb, it will still give that error, because boolean is just tinyint(1)
if it is sqlite, it will just ram the empty string into the number column
I swear that there is a common db that allows actual boolean columns... doesn't postgres or something? It's the only other one I've used regularly
probably
never used it
you can cast to integer before saving to the database, something like:
intval($_POST['value'], 10)
nah its all good
ill just do 1 and 0 instead of true n false in the backend, easiest solution + it doesn't need to do any converting
still odd that dbs dont actually use bools though
I mean to be fair in the backend a bool is 1 or 0 anyway so it makes sense to save it as just 1 char
by the way, if you set a value to the checkbox, it will send the value instead of "on"