Hi,
I want blank textbox value to be inserted into the database.
For that I kept those fields 'default NULL' in mysql. Still doesn't take empty textboxes to be saved blank. What should I do? where am I wrong?

Recommended Answers

All 11 Replies

What type is the field in your database?

Wow, can you please give us more explanation on what you are trying to achieved here.

I thought when you do this;

INSERT INTO `YourTable`(`col-1`, `col-2`, `col-3`) VALUES ("",[value-2],[value-3])

That should give you an empty value for the col-1.. or you can set the table null -> Yes and then default -> NULL .

my one field is varchar(50) and other is 4 fields are decimal.
doing,

       public function Insert()
      {
        $this->_abc = $_POST["txtabc"];
        $this->_bcd = $_POST["txtbcd"];
        $this->_cde = $_POST["txtcde"];
        $this->_def = $_POST["txtdef"];
        $this->_efg = $_POST["txtefg"];
        $this->_fgh = $_POST["txtfgh"];
        $this->_ghi = $_POST["txtghi"];
        $this->_hij = $_POST["txthij"];
        $this->_amount = $_POST["txtamount"];



     $this->sql_insert = 'INSERT INTO tablename(ABC,BCD,CDE,DEF,EFG,FGH,GHI,HIJ,IJK) values'.'("'.$this->_abc.'",'.$this->_bcd',"'.$this->_cde.'","'.$this->_def.'","'.$this->_efg.'","'.$this->_fgh.'",'.$this->_ghi.','.$this->_hij.','.$this->_ijk.')';

$this->mq = mysql_query($this->sql_insert) or die(mysql_error());
   }

I want last 4 fields to be saved blank.one is varchar and other 3 are decimal.

Decimal fields won't like empty values being passed even if they are set to NULL so you will need to run some checks and apply a value if the posted data is empty, for example

$this->_amount = !empty($_POST["txtamount"]) ? $_POST["txtamount"] : NULL;

ok
but then also i'm getting an sql error.

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',)' at line 1"

Without null it takes my values.That means still there's something missing. what else could it be?

For a start, this is wrong values'.'(
I don't know what you are trying to do their but it will break your script. You are also mixing styles of quote marks in your value parenthesis so change them all to be the same.

Try this:

"INSERT INTO tablename(ABC,BCD,CDE,DEF,EFG,FGH,GHI,HIJ,IJK) values('".$this->_abc."','".$this->_bcd."','".$this->_cde."','".$this->_def."','".$this->_efg."','".$this->_fgh."','".$this->_ghi."','".$this->_hij."','".$this->_ijk."')";

I have put the right sql syntax otherwise it couldnt save the form. it inserts my values only when I dont want those 4 values blank(NULL). it also saves my varchar with null but WHAT ABOUT DECIMAL. APPLIED MANY WAYS BUT FAILED TO SAVE THE FORM WITH NULL IN DECIMALS.

But it obviously isn't working properly otherwise you wouldn't have hot the error you mentioned. Set the default value for your decimal fields as '0.00' instead of null and see if that helps.

And don't use capitals - there is no need to shout when someone is trying to help you.

I apologize if you feel so .I was merely highlighting my part of sentense where I'm stuck.

well I converted my field type from decimal to int as I only need int values instead of decimal. insertion of those fields with null values from textbox isn't done. I will try if I can change something and get it done. Thank you all for helping me really. and please do suggest on this if that can solve my issue.

thank you. my work is done with little changes in the value...0 intead of NULL in ternary operator.
Thank you all once again helping me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.