Solved

PHP MySQL Integrity constraint violation

Posted on 2009-07-04
4
706 Views
Last Modified: 2013-12-13
Error Message: Debug error while inserting into database: 1SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 1

My table schema:
ID[INT Primary Key] Auto increments
boxNumber[INT]
show[TINYINT UNSIGNED Nullable]
description[TEXT Nullable]
url[VARCHAR(100) Nullable]
purchased[TIMESTAMP]

There is only one record in the table with ID (primary key) of zero. Also, that record has a boxNumber of zero, even though I inserted a 1... not sure if that's related.

I am a TOTAL newbie when it comes to MySQL. I am using Eclipse with Zend on XP. If you need any other information, tell me what you need and how I go about getting it.

Thank you all VERY much.
$DatabaseObj = Zend_Registry::get('Instance/DatabaseObj');
$DatabaseObj->beginTransaction();
 
$data = array(
    	'description'    => $description,
    	'url' 		 => $urlAddress,
    	'purchased'      => $today,
	'boxNumber'      => $boxNumber);
 
// This line causes the exception...
$DatabaseObj->insert('tblImages', $data); 
 
// Therefore, this line never executes.
$DatabaseObj->commit();

Open in new window

0
Comment
Question by:yaronusa
[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
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 24777029
where did you set $boxNumber

try hard coding the boxNumber (just once) and see if you still get the error. This would tell you that's it's something in the insert function.
0
 

Author Comment

by:yaronusa
ID: 24777049
I have tried to pass $boxNumber as other numbers but the insert statment is still throwing an exception.

The single row/record in the table has an ID of zero, and remember ID auto increments. So the second record I try to insert throws an exception, that I'm trying to insert another row/record with a duplicate ID of zero.

Notice I am not including the ID in the insert statement, that should automatically be provided and incremented, I thought.

0
 
LVL 19

Accepted Solution

by:
Michael701 earned 500 total points
ID: 24777163
try setting it. if it works then something is wrong with the way you defined the table.

do you have mysql admin? can you
describe table_name;
$data = array(
        'ID'             => 1,
        'description'    => $description,
        'url'            => $urlAddress,
        'purchased'      => $today,
        'boxNumber'      => $boxNumber);

Open in new window

0
 

Author Closing Comment

by:yaronusa
ID: 31599763
Actually, the column was not set to auto increment.... my fault.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

752 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