Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/165035-inserting-when-null/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870299
Share on other sites

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)?

Link to comment
https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870306
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870308
Share on other sites

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.  :)

Link to comment
https://forums.phpfreaks.com/topic/165035-inserting-when-null/#findComment-870310
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.