Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:21:54+00:00 2026-05-28T06:21:54+00:00

How can I insert a NULL value into a database field (MySQL) with char(2)?

  • 0

How can I insert a NULL value into a database field (MySQL) with char(2)?

Before I used mysql_query and I got in the database the following value: NU

Instead of this value there should be NULL.

After reading this question I switched to prepared statements. I still have NU in my database.

Now to my code. I’m reading out line for line of a CSV. Then I make

    if(empty($data[$c])) {
        $data[$c] = NULL;
    }

If I echo $data[1] I get NULL. Then I make my prepared query:

$stmt = $connection->prepare("INSERT INTO table (first, second, ..) VALUES (?, ?, ...)");
$stmt->bind_param("ss...", $data[0], $data[1], ...);
$res=$stmt->execute();

What is wrong?

Edit:

According to Shiplu I did the following:

Do it without giving the last params which causes NU:

$stmt = $connection->prepare("INSERT INTO employee (first, second, ...) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssssssssii", $data[0], $data[1], ..., $last);

Using default:

$stmt = $connection->prepare("INSERT INTO employee (first, second, ..., last) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, default(last))");
$stmt->bind_param("sssssssssssssii", $data[0], $data[1], ...);

But still the same result. The char(2) field has default = NULL. How can that be? It seems that the problem is not with the query. I think the easiest solution would be an update query …

Create table:

CREATE TABLE IF NOT EXISTS `mytable` (
  `id` int(11) NOT NULL auto_increment,
  `text` varchar(128) default NULL,
  `no` int(11) default NULL,
  `problemfield` char(2) default NULL,
  PRIMARY KEY  (`id`),
  FULLTEXT KEY `text` (`text`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=832 ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:21:55+00:00Added an answer on May 28, 2026 at 6:21 am

    I found the problem. I was reading out a csv file. This file was exported with phpmyadmin and it contained NULL strings. My check

    if(empty($data[$c])) {
        $data[$c] = NULL;
    }
    

    wasn’t enough, because it never checked for the string NULL. So the solution is:

    if(empty($data[$c]) || $data[$c]=='NULL') {
        $data[$c] = NULL;
    }
    

    So Andrew was right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a null value into a database if the form field
How i can insert data into more than one table of an Access database
I am getting a 'Cannot insert the value NULL into column...' error when trying
Is there a way that I can insert values into a VB.NET Dictionary when
Is '' a valid piece of XML? Can '' insert into oracle xblob column
I store user editable articles in a database. Users can insert some simple widgets
I have three tables in a MySQL database used in a music library application:
In my SQL database there're many fields like this: Field Type:Text Null:Yes Default:NULL My
I'm migrating some data into MySQL and the dump from the other database has
I have created an INSTEAD OF INSERT trigger on a view in my database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.