Php+mysql throw an exception when inserting duplicate data, can you catch an exception?

Source: Internet
Author: User

I mean can be inserted to return true, can not return false, but can not be inserted when the direct display of this

Negative on My Code:

Reply content:

I mean can be inserted to return true, can not return false, but can not be inserted when the direct display of this

Negative on My Code:

Naturally, it is possible.
You can use the following three ways to make a page jump in combination with your business needs

Ignore: A repetition is ignored.

INSERT IGNORE INTO `table_name` (`email`, `phone`, `user_id`) VALUES ('test9@163.com', '99999', '9999');

Replace: Delete old data and add new data if duplicate

REPLACE INTO `table_name`(`col_name`, ...) VALUES (...);REPLACE INTO `table_name` (`col_name`, ...) SELECT ...;REPLACE INTO `table_name` SET `col_name`='value'

On duplicate key update: Duplicate update

INSERT INTO `table` (`a`, `b`, `c`) VALUES (1, 2, 3), (4, 5, 6) ON DUPLICATE KEY UPDATE `c`=VALUES(`a`)+VALUES(`b`);

Recommended article: MySQL avoid duplicate insert record method (Ignore,replace,on DUPLICATE KEY UPDATE)

You can take this exception Catch yourself, and then return TRUE or False

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.