bye_nary Posted December 18, 2009 Share Posted December 18, 2009 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. Link to comment https://forums.phpfreaks.com/topic/185577-putting-the-data-of-a-php-variable-into-a-float-field-in-mysql-that-is-null/ Share on other sites More sharing options...
cags Posted December 18, 2009 Share Posted December 18, 2009 Try... }else{ $_some_data = 'NULL'; } Link to comment https://forums.phpfreaks.com/topic/185577-putting-the-data-of-a-php-variable-into-a-float-field-in-mysql-that-is-null/#findComment-979743 Share on other sites More sharing options...
japesu Posted December 18, 2009 Share Posted December 18, 2009 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. Link to comment https://forums.phpfreaks.com/topic/185577-putting-the-data-of-a-php-variable-into-a-float-field-in-mysql-that-is-null/#findComment-979745 Share on other sites More sharing options...
bye_nary Posted December 18, 2009 Author Share Posted December 18, 2009 Hi. Thanks for the suggestion but that did not work either. I have tried various forms of the null/NULL value and none seem to work... :-\ Link to comment https://forums.phpfreaks.com/topic/185577-putting-the-data-of-a-php-variable-into-a-float-field-in-mysql-that-is-null/#findComment-979746 Share on other sites More sharing options...
bye_nary Posted December 18, 2009 Author Share Posted December 18, 2009 Ah yes I see. I shall try that. Thanks. Link to comment https://forums.phpfreaks.com/topic/185577-putting-the-data-of-a-php-variable-into-a-float-field-in-mysql-that-is-null/#findComment-979747 Share on other sites More sharing options...
cags Posted December 18, 2009 Share Posted December 18, 2009 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})"); Link to comment https://forums.phpfreaks.com/topic/185577-putting-the-data-of-a-php-variable-into-a-float-field-in-mysql-that-is-null/#findComment-979766 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.