user765368
user765368

Reputation: 20356

cannot save null values for type INT (php, symfony)

I have a question here. I have a website running on symfony 1.0. I used the symfony admin generator tool to generate an interface to edit information in the database. On the edit page, I have some textfields that require integer values to be saved in the database. The problem that I'm experiencing is that, if I don't put anything in the textfields that require integer numbers and hit SAVE, the value of ZERO is saved in the database (instead of null). Keep in mind that in the structure of the database, the type of the columns where the integers are saved are type INT(11) DEFAULT NULL. So, normally, NULL should be saved if I don't enter anything.

Any idea what might go wrong please?

Upvotes: 0

Views: 1079

Answers (1)

Madara's Ghost
Madara's Ghost

Reputation: 175048

Yes, form fields are always Strings. So when you submit them without inputting anything, you are actually submitting '' (an empty string) which gets translated into 0. You should include a simple condition to make sure the input is not empty, and take appropriate action if it does.

if (empty($_POST['input'])) { $variable_to_pass = null; }

Upvotes: 5

Related Questions