Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of inserting the same data in Thinkphp3.2

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to solve the problem of inserting the same data in Thinkphp3.2". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the problem of inserting the same data in Thinkphp3.2".

Problem description

Today, when using TP3.2 to insert data, in order to avoid inserting the same data (the so-called same data, the same primary key or the same field of the unique index), the index I created is like the following figure, the primary key index is a self-increasing field, and there can be no repetition, that is, there may be duplicates in the unique index. I hope that if the three fields of uid,year,mounth,day are the same, update the current record.

Solution to the problem

When faced with such a problem before, we know that MySQL provides ON DUPLICATE KEY UPDATE or REPLACE INTO to solve it.

Use ON DUPLICATE KEY UPDATE

Before inserting data, there is only one record in the table, as shown in the following figure

The SQL statement is as follows: when the record is inserted, it is the same as the record already in the table, the change record is updated, otherwise the record is inserted.

INSERT INTO `status` (`uid`, `year`, `mounth`, `day`, `status`) VALUES (1, 2016, 6, 3, 1) ON DUPLICATE KEY UPDATE `status` = VALUES (`status`), `updated_ ts` = NOW ()

Use REPLACE INTO

The code is as follows:

First execute the following code to insert a piece of data

REPLACE INTO `work_ log` (`uid`, `year`, `mounth`, `day`, `status`) VALUES (1, 2016, 6, 2, 1)

The effect is as follows

Execute the following code again, and the code inserted above will be updated

REPLACE INTO `work_ log` (`uid`, `year`, `mounth`, `day`, `status`) VALUES (1, 2016, 6, 2, 5)

The effect is as follows

The difference between ON DUPLICATE KEY UPDATE and REPLACE INTO

When the same value occurs, ON DUPLICATE KEY UPDATE updates the existing record, and REPLACE INTO deletes the previous record and then inserts the new record.

Solution in Thinkphp3.2

In Thinkphp3.2, the problem of inserting the same data is handled by the third argument of the add () function.

The add () method in Model.class.PHP calls the method in insert in Db.class.php. In the insert method, we can see the following code:

Where $replace happens to be the third parameter in the add method.

Thank you for your reading, the above is the content of "how to solve the problem of inserting the same data in Thinkphp3.2". After the study of this article, I believe you have a deeper understanding of how to solve the problem of inserting the same data in Thinkphp3.2, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report