Jump to content

PHP Defaults to boolean from Yes / No


trvo

Recommended Posts

Hi Guys,

 

I'm new here and looking for help with an application that I am developing. Basically my application collates data over several pages of forms in an ini file and then puts it in the mysql database.

 

I have some Yes / No radio buttons on some of these forms which populate the .ini file with the word Yes or No. So up to this stage it all works fine. Now, when I retrieve the data php, for some reason, converts Yes or No into a boolean value. It also does this when retrieving the data back from mysql after I have written the word 'Yes' / 'No' to the database. I can over come this each time I reference it but I'd rather it worked fine in the first place to prevent any complications in the future. Can anyone suggest why this might be happening and how I can tell PHP not to change my data into what it 'thinks' is correct?

 

Many Thanks - Trev

Link to comment
Share on other sites

To access the .ini data I am using this:

 

INI file:

[Options]
field=Yes

 

PHP Code:

// Build an array of data from the ini file
$ini_array = parse_ini_file($ini_filename);

// Output the data for the field: 'field'
echo $ini_array['field']; // Outputs a 1

 

The MySQL code is as follows (the content of field is 'Yes' but outputs a 1):

$id=$_GET['id'];

	$query = "SELECT * FROM `requests` WHERE `id`='".$id."'";
	$result=mysql_query($query) or die(mysql_error());

	$field=mysql_result($result,0,"field");

 

Many Thanks - Trev

Link to comment
Share on other sites

Why don't you give the radio buttons a value of YES and NO in the HTML rather than getting PHP to set the value in the php.ini

 

Do you agree? Yes: <input type="radio" name="confirm" value="Yes" />   
<input type="radio" name="confirm" value="No" />

 

That way when when you access the data submitted from the form which ever radio button the user submitted it will return the string 'Yes' or 'No' for $_POST['confirm'] rather than true or false.

 

Remember radio buttons can only submit 1 value from the same group of radio buttons. You must name every radio button that belongs to a group the same name.

Link to comment
Share on other sites

That is currently how it works...

 

User submits HTML form > PHP creates .ini file > More forms add more data to .ini file > .ini file reads back with 1 / 0 in the Yes / No fields when pulled back via php. Yes / No fields are always radios. However, when you actually open the newly created .ini it has the correct value of Yes / No.

 

Trev

Link to comment
Share on other sites

Note:  There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, and false. Values null, no and false results in "", yes and true results in "1". Characters {}|&~![()" must not be used anywhere in the key and have a special meaning in the value.

Link to comment
Share on other sites

Regarding your mysql .. are you 100% sure that you are having "Yes" and "No" converted to bools?  Did you check them before you put them into the database, while they are in the database and also when you took them out?

 

The reason I am suspicious is that SQL interfaces, including php's mysql interface, always return strings.  You can see this in the return type on this page:

 

http://sg.php.net/manual/en/function.mysql-result.php

 

The function cannot return a boolean.  Additionally, Mysql does not even have a boolean data type.  It uses integers.  Can you try var_dump($field) immediately after the code you posted?

Link to comment
Share on other sites

Sorry, that's my mistake, I'm quite deep into this, I was getting confused as to which pages pull data from where...Best get organised...I'm up to 200 9kb-ish pages so far and counting  :o

 

Thanks - Trev

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.