MySQL storing boolean - 1, 0 , or NULL - php

So I've generally stored Boolean values in MyIASM MySQL DBs using TinytInt(1). In a site I'm currently working on, I need to be able to store 1, 0 or NULL. This field is being populated from a radio button in a PHP form.
When I choose 'Yes' in the form (with a value of 1), this gets stored accurately. When I choose 'No' however (with a value of 0), it gets stored as NULL.
I want to reserve NULL for if the user chooses neither 'Yes' or 'No'.
Any idea why the 'No' (0) values aren't storing as expected?
EDIT:
Here's the basic HTML:
Yes <input type='radio' name='video_transfer_dvd_question' value='1' />
No <input type='radio' name='video_transfer_dvd_question' value='0' />

In PHP, both '' and 0 are considered FALSE in a boolean context, so if you try to test '' == 0 you'll find the result is TRUE. And empty() only checks whether the argument is boolean FALSE, so it doesn't truly distinguish between an empty $_POST and a $_POST which contains a value that happens to evaluate as FALSE. My suggestion would be to change the values of your radio buttons so than they never evaluate as boolean FALSE, e.g.:
Yes <input type='radio' name='video_transfer_dvd_question' value='yes' />
No <input type='radio' name='video_transfer_dvd_question' value='no' />
This keeps things more explicit when you test the response and choose a value to send to MySQL:
if (empty($_POST['button'])) {
$value = NULL;
} elseif ($_POST['button'] == 'no') {
$value = 0;
} elseif ($_POST['button'] == 'yes') {
$value = 1;
}
You could also use !$_POST['button'] in place of empty($_POST['button']), but that would generate a warning if $_POST['button'] is not set, which you would probably rather not happen.
You could also change that last elseif block to a simple else, if you have no reason to expect that the possible responses to this radio button will ever change and don't care about being explicit.

As the other people have commented its 99% an issue with your code...what you will need is something like...
if ($_POST["radiobutton"]) { $insertValue = 1; } else { $insertValue = 0; }
or
$insertValue = ($_POST["radiobutton"]) ? 1 : 0;
The reason is an empty radio button is an empty $POST var value which gets interpreted as NULL in mysql.

Related

How to make a radio button return boolean true/false instead of on/off

I want that my radio buttons return me a Boolean value true or false instade of on/off
So I pass the true/false in the value of the input :
<label>Male
<input type="radio" name="IsMale" value="true" />
</label>
<label>Female
<input type="radio" name="IsMale" value="false" />
</label>
but it returns me a true/false in a text format. Please masters how could I get them in a booleen format ?
More details : In fact I need to store my $_POST array in a file.txt, and for my radio button I need to store for example :
array ( "IsMale" => true );
and not :
array ( "IsMale" => "true" );
You'll have to check the data and modify it.
if(isset($_POST['SubmitButton'])) {
$_POST['IsMale'] = $_POST['IsMale'] == 'true' ? true : false;
}
You cannot make radio buttons or any other form element directly submit a PHP true value, only a string such as "true".
To solve your problem, you would have to change the value of the $_POST item in your PHP file.
//Form has been submitted
if(isset($_POST['submit'])) {
//Radio button has been set to "true"
if(isset($_POST['IsMale']) && $_POST['IsMale'] == 'true') $_POST['IsMale'] = TRUE;
//Radio button has been set to "false" or a value was not selected
else $_POST['IsMale'] = FALSE;
}
Edit: Ben has provided a functional solution using ternary operators which is a shorter alternative. The example above may clarify exactly what is going on in the process (in a more verbose form).

Data field null value return 0

I am storing a radio button value which can be true, false or NULL in a database.
In case of null or false the answer is 0 that is false.
Is there any solution for checking the null value? I have tried using isset and Empty but none of these help.
Database is MSSQL and datatype is bit to store the value of radio button
<input name="radioVal" type="radio" id="radioVal" value="false"
<?PHP
if(row['radioVal'] == false )
echo "checked='checked' ";
}
?> />
<input name="radioVal" type="radio" id="radioVal" value="true"
<?PHP
if(row['radioVal'] == true )
echo "checked='checked' ";
}
?> />
TRY
if(false === row['radioVal'] ) //or
if(NULL === row['radioVal'] )
and to check for NULL you can check with is_null
is_null(row['radioVal'])
The bit column is probably going to be represented as a 1 or a 0. Use the triple equals to check for value and type. Check if row['radioVal'] === 1 for true, row['radioVal'] === 0 for false, and row['radioVal'] === null for null.
Maybe you should use another data type in your SQL Database.
I suggest Enum or something like that:
ENUM('false', 'true', 'NULL')
In your PHP file you
<?PHP
if(row['radioVal'] == false )
echo "checked='checked' ";
}
?>

reflecting a mySQL boolean in a checkbox

I have a field in mySQL featured, TINYINT, which will be either 0 or 1
I also have a form:
<input type="checkbox" name="featured" value="<?php echo $row_rs_dealItem['featured']; ?>"/><br /><br />
When I select the checkbox I am able to set the value to '1' in mySQL, however not the other way round because when I view the update record page the checkbox is always blank (I understand the checkbox status is independent of the value and should be set so).
I therefore need the checkbox to be checked if value '1', and also if I uncheck, then I need the value in mySQL to change back to '0'
I know there are lots of posts on this, but I can't seem to make head nor tail, and nothing I try works.
Can we keep it in PHP please?
thanks
use it in your html checkbox:
<?php if ($row_rs_dealItem['featured'] == 1) echo "checked='checked'"; ?>
and write it in your file where you save the data:
if(isset($_POST['featured'])) $featured = 1; else $featured = 0;
$featured = (isset($_POST['featured'])) ? 1 : 0;
Just use PHP's intval function,
after you submit the form, in the file where you save the data:
$_POST['featured'] = intval($_POST['featured']);
If checkbox isn't checked $_POST['featured'] is NULL, and intval(NULL)=0.
try
<?PHP
if(isset($_POST['featured']))
{
$_POST['featured'] =1;
}
else
{
$_POST['featured'] =0;
}

Cant get radio button value to post in php

I started with the code from simplemodal, and modified to suit my needs. That went amazingly well. The last issue I have is getting some radio buttons to post. I'll skip the code blocks I don't think are necessary and just show what I think is relevant. I've tried literally dozens of attempts of solutions from php.net, but nothing seems to work.
HTML
<label for='PayPlatform'>Are you willing to pay for a trading platform?</label>
<input type='radio' name='PayPlatform' value='Yes' tabindex='1001' />Yes
<input type='radio' name='PayPlatform' value='No' tabindex='1002' />No
Here's where I can't get the value and my attempt there are two attempts in the codeblock below, they were of course not attempted at the same time.
else if ($action == "send") {
// other elements form values are retrieved fine, just not the below
$PayPlatform = isset($_POST['PayPlatform']) ? ' checked="checked"' : "";
//the ternary above just submits 'checked="checked" no matter which radio is checked
//another attempt
$PayPlatform = isset($_POST["PayPlatform"]);
//this just submits "1" weather yes or no is checked
$token = isset($_POST["token"]) ? $_POST["token"] : "";
// make sure the token matches
if ($token === smcf_token($to)) {
smcf_send($name, $email, $subject, $phone, $message, $PayPlatform);
echo "Your message was successfully sent, you can close this window";
}
else {
echo "Unfortunately, your message could not be verified.";
}
}
$p = (isset($_POST['PayPlatform']) && $_POST['PayPlatform'] == 'Yes');
Boolean true if the yes radio is checked, and false otherwise.
Assume you have the following:
$a = 'Yes';
isset($a); //true
$b = 'No';
isset($b); //true
Strings are considered set, and as PsyCoder said, you'll receive the value values, not Booleans.
Also, on a different (chastise-y) note, this question could have been entirely avoided with a bit of better debugging. The first thing I would have done would have been to var_dump($_POST) and see what the value was. You'd have then seen that $_POST['PayPlatform'] was always Yes or No, and you'd have potentially realized that isset is always true on strings.
You wont get value as checked or unchecked... rather you'll receive the value='No' or value='No' parameter value specified in your html...
if($_POST["PayPlatform"] == "Yes") {
$PayPlatform = true;
}
Keeping it simple :)

PHP not retrieving radio input value

I'm having a real problem with retrieving the value of a radio button using PHP . . . I have two radio buttons as such:
<input name="admin" type="radio" value="1" />Yes
<input name="admin" type="radio" value="0" checked />No
And a conditional PHP statement checking to see if PHP can retrieve any data from it:
if(!empty($_POST['admin'])) {
// do stuff
}
else {
echo "Value not set";
}
the problem is that PHP can seem to return a value for the radio buttons if "yes" is selected, but not if "no" is selected, I've tried removing the "checked" portion, to no avail. I just can't get it to retrieve the "0" value whatever I try.
I remember using PHP arrays to name checkboxes, but this shouldn't be needed for radio buttons surely, as only can be selected at any one time?
Or does PHP just have a problem returning radio buttons with a value of 0?
Or am I doing something horribly wrong without realising it?
PHP function empty will return false for 0, NULL, "", and others.
Quoting from php.net:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
All of these will return (false) therefore nullifying the execution of your if statement.
You will have to modify your condition to reflect a value depending on what you wish to accomplish.
Good luck!
Your problem is that empty() treats zeros as empty values, no matter if it's a number 0, or a string '0'. So, instead of using empty() use isset() and/or direct checks, e.g.
if (isset($_POST['admin'])) {
or
if (isset($_POST['admin']) && $_POST['admin'] == 0) {
I believe the zero is your problem. Try setting them to "Y" and "N" and you should see that the N value comes through. This is because 0 is an empty value in PHP.

Categories