Solved

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

Posted on 2014-12-19
3
164 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
3 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 50 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 450 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL Grouping 2 41
mysql between clause 2 24
How to properly use session_regenerate_id when user logs in 7 40
Insert values are dynamic 11 41
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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