Jump to content

[SOLVED] why is field in mysql blank?


dsaba

Recommended Posts

I am entering the values of variables into a mysql database

I know that some values of the variables are GOING to be blank

 

so to prep my mysql database tried 2 different things:

1. i set those fieldnames to "NULL" in mysql with phpmyadmin, and also put a default value

2. i set those fieldnames in mysql to "NOT NULL" and still put a default value

 

 

well when the value of the variable i'm entering into a fieldname is in fact NULL

 

it shows up blank in my database ALL the time, with either of the two methods i tried

the default values i set in phpmyadmin DOES NOT SHOW UP for either method

 

 

so my question is how do I make this work, and why is this happening?

-thank u

 

and if you're wondering if my variables are not being type correctly in the first place

THEY ARE FINE

because when the value of the variable is NOT NULL, the mysql fieldnames of those entered values show up fine

 

 

i'm doing all this on INSERT also if thats any help

Link to comment
Share on other sites

Are you aware that there is a difference between an empty string and null?

 

Edit: To insert null into a database, you must write the word "null", or leave the column unspecified altogether.  If you insert a blank string value into the database, then you will get a blank string, not null.

Link to comment
Share on other sites

my mysql version is 4.1.18

 

here is the part where I assign a value to the variable:

$svideoifwalla = $_POST['svideoifwalla'];

 

the post value can either be NULL or empy string (as I dont know the difference)

or it can be "Yes"

 

here is the part where I insert it into my db:

    $query1 = "INSERT INTO `mainfilelist` ( `fileifwalla` )

VALUES ( '$svideoifwalla' )";

 

$runquery1 = mysql_query( $query1 );

 

 

Link to comment
Share on other sites

ok so what I am doing then is inserting a BLANK STRING

 

what I want is my fieldname to have a default value in the case that it is a blank string

 

do I set the fieldname to NULL and put a default?

 

OR do i set the fieldname to NOT NULL and put a default?

Link to comment
Share on other sites

Default values are only used when you leave the field unspecified.  That is, if you don't mention it at all in your insert statement.

 

For your case, it may be better to set the default value in php like this:

 

if (empty($svideoifwalla)) {
  $query1 = "INSERT INTO `mainfilelist` ( `fileifwalla` ) values ( 'your_default_value' )";
} else {
  $query1 = "INSERT INTO `mainfilelist` ( `fileifwalla` ) values ( $svideoifwalla )";
}

 

empty() will tell you if nothing was entered into the form field.  An empty form field will always have a blank string value, rather than null.

 

If you do it this way, it doesn't matter if your field is null or not null, and the default value (in sql) is not used.

Link to comment
Share on other sites

ok so you're saying that if you are entering a value with a blank or null string in your INSERT statement

THEN there is not way you can make mysql automatically set a default value for that field

 

 

and therefore:

the ONLY WAY you can make mysql set default values is when you DO NOT MENTION THEM at all in your insert statements

 

 

is that correct?

Link to comment
Share on other sites

yes

 

 

if (empty($svideoifwalla)) { // if this variable is empty then in the INSERT DO NOT MENTION IT

 

$query1 = "INSERT INTO `mainfilelist` ( `fileifwalla` ) values ( 'your_default_value' )";

 

} else { // else if it is NOT empty then mention it here and do what btherl has below

 

  $query1 = "INSERT INTO `mainfilelist` ( `fileifwalla` ) values ( $svideoifwalla )";

 

}

 

 

 

then you can set a default for that particular column in your table

Link to comment
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.