2

I am new to PHP and I am developing a project using http://www.php-mvc.net/ light php MVC framework. I have a many to many mapping between two tables i.e

collection : id
profile : id
collection_profile : collection_id <--> profile_id

I have created a function where I insert mapping row in the collection_profile table. Now the problem. I have create composite key of collection_id and profile_id so it wont let me enter duplication combination, that is what i want.

I have added these lines in try,catch block. but It still shows Warning: PDOStatement::execute(): SQLSTATE[23000]: Integrity constraint violation on the page.

I found error_reporting(E_ERROR) but I am not able to figure out where to put this line. because I want to disable warning only for that function.

BTW, I am doing this because. I don't want to check if the row exist and then try to insert. I want mysql to do the work.

Let me know if you require anymore information.

Thanks in advance.

1 Answer 1

2

In PHP, typing @ before the statement lets you suppress any warnings it produces

@$stm->execute();

But still it would be much better if you understood why exactly the warning is issued. You're probably doing something wrong.

Edit: You can suppress this warning on MySQL side by executing INSERT IGNORE instead of INSERT, see http://dev.mysql.com/doc/refman/5.6/en/insert.html

Sign up to request clarification or add additional context in comments.

6 Comments

I will try it. I am doing this because. I don't want to check if the combination does not exist and then try to insert. I want to let mysql do the work.
In this case, since you're using MySQL, you can do INSERT IGNORE to suppress the warning on MySQL side
I will do that. That is a better solution that adding @. Thanks Again.
I don't need to wrap the code try-catch then, right?
@AmitChotaliya right, no exceptions will be thrown. MySQL will simply do nothing, if the row already exists
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.