Jump to content

Putting the data of a php variable into a float field in MySQL that is NULL


bye_nary

Recommended Posts

Hi,

 

I have a form that users can enter their data into. This form has a handler and that handler takes the data and inserts it into a MySQL table.

 

However, the problem I am having is that there are fields in the MySQL table defined as a FLOAT and NULL IS allowed. When I request the data from a textbox that has not been filled in I get the message:

 

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in <file_name>

 

The code that obtains the data is of the format:

if (!empty($_REQUEST['textbox_text'])){
      $_some_data = $_REQUEST['textbox_text']
}else{
      $_some_data = NULL;
}

 

When I try and add this variable to the list of variables in the query whose data should be added to the table I get the error above.

 

Any ideas? Please let me know if you need to know more.

 

Thanks.

Just don't insert anything to that column if you want it to be left as NULL value in database. So you will need to make your INSERT clause different depending on what values the user wants to insert. One way would be to form it dynamically by looping through the keys and values of the REQUEST array user has sent.

 

Not submitting it, is certainly the simplest way, but I'm certain $var = 'NULL'; is the correct way of doing it, if that is not working, the problem is likely the SQL statement itself, but you didn't provide us with that. Using $var = NULL, will not work because the PHP NULL construct/syntax is not compatible with being sent via a string in a MySQL query. It should be sent as...

 

mysql_query("INSERT INTO table (field1, field2) VALUES('bob', NULL)");
// as a variable...
$var = 'NULL';
mysql_query("INSERT INTO table (field1, field2) VALUES('bob', {$var})");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.