?
Solved

Transitioning to PDO/MySQL.  Need help  with error. Column cannot be null

Posted on 2014-12-19
3
Medium Priority
?
180 Views
Last Modified: 2014-12-19
I am getting an error
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'specs6' cannot be null'

How do you allow null values in PDO?

$statement = $conn->prepare("INSERT INTO specsdownload(sku,specs1,specs2,specs3,specs4,specs5,specs6) VALUES(:sku,:specs1,:specs2,:specs3,:specs4,:specs5,:specs6)");
$statement->execute(array(
    "sku" => $data[0],
       "specs1" => $data[1],
	"specs2" => $data[2],
	"specs3" => $data[3],
	"specs4" => $data[4],
        "specs5" => $data[5],
	"specs6" => $data[6]
));

Open in new window

0
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 200 total points
ID: 40510278
In your data table definition, 'specs6' is probably defined as 'Not Null'.  So in "specs6" => $data[6] you need to provide a default value for $data[6] so that it is never null.
0
 
LVL 58

Accepted Solution

by:
Gary earned 1800 total points
ID: 40510317
Just to follow on from Dave correct observation, to get around the error replace

    "specs6" => $data[6]

with

    "specs6" => isset($data[6])?$data[6]:""

That will just add a blank value to the field, else allow IsNull in the field
0
 

Author Closing Comment

by:lawrence_dev
ID: 40510393
Worked great!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question