RynMan Posted July 7, 2009 Share Posted July 7, 2009 Hey guys So I've built a form with various text fields, radio buttons, and text areas. Now I want to be able to use the data collected from that form and INSERT it into my table using PHP. On the next page (the action=) page from my form, I set up the INSERT query, however it runs into a problem when I don't enter a value into a field on the form on the previous page. It's because I'm using the $variable = $_POST["postedvariable"] method, and since nothing was entered, nothing is posted, and the query fails since there is no value. I'm wondering how this is usually handled in PHP? Do you have to write 'if statements' to handle it? Thanks for any help guys! Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/ Share on other sites More sharing options...
Bendude14 Posted July 7, 2009 Share Posted July 7, 2009 try this if(isset($_POST["postedvariable"])) { $variable = $_POST["postedvariable"]; } Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870291 Share on other sites More sharing options...
RynMan Posted July 7, 2009 Author Share Posted July 7, 2009 try this if(isset($_POST["postedvariable"])) { $variable = $_POST["postedvariable"]; } Thanks Ben. But what now happens if the value of $_POST["postedvariable"] is null? Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870296 Share on other sites More sharing options...
Bendude14 Posted July 7, 2009 Share Posted July 7, 2009 well isset() checks if the variable was ever set. if $_POST["postedvariable"] is empty you can check this also like so if(empty($_POST["postedvariable"])) { //more code } if $_POST["postedvariable"] actually contains the value null you will have to check for this seperatly like so if($_POST["postedvariable"] == null) { //more code } with these examples you should be able to put them together and take action accordingly... Ben Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870299 Share on other sites More sharing options...
RynMan Posted July 7, 2009 Author Share Posted July 7, 2009 well isset() checks if the variable was ever set. if $_POST["postedvariable"] is empty you can check this also like so if(empty($_POST["postedvariable"])) { //more code } if $_POST["postedvariable"] actually contains the value null you will have to check for this seperatly like so if($_POST["postedvariable"] == null) { //more code } with these examples you should be able to put them together and take action accordingly... Ben I see. I've elected to try this... if(empty($Gender)) $Gender == null; else $Gender = $_POST["Gender"]; However it still doesn't like it because $Gender has nothing assigned to it. Instead of null I also tried '' however that didn't work either. What can I put there that both satisfy the PHP (the variable is defined) and the mySQL (something to insert)? Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870306 Share on other sites More sharing options...
Bendude14 Posted July 7, 2009 Share Posted July 7, 2009 do you have a constraint on you table in mysql that does now allow null as a value? because otherwise that should work... If you check and their are no constraints on the column post your query here... When you say it doesn't work, what errors are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870308 Share on other sites More sharing options...
RynMan Posted July 7, 2009 Author Share Posted July 7, 2009 do you have a constraint on you table in mysql that does now allow null as a value? because otherwise that should work... If you check and their are no constraints on the column post your query here... When you say it doesn't work, what errors are you getting? I'm using PHPMyAdmin and in my table structure, almost all of my fields have "No" under the "NULL" column. I'm guessing I need to change them to yes. Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870310 Share on other sites More sharing options...
Daniel0 Posted July 7, 2009 Share Posted July 7, 2009 The $_POST array cannot contain null values unless you put them there manually. Even an empty field submitted via a form won't be null, but rather an empty string. Those two things aren't the same. Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870344 Share on other sites More sharing options...
Bendude14 Posted July 8, 2009 Share Posted July 8, 2009 yes you need to change them to allow null as a value. As daniel said for having empty strings you are checking against that already with this code. if(empty($Gender)) $Gender == null; else $Gender = $_POST["Gender"]; Quote Link to comment https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.