I’m trying to create a column and make it count the number of tries of insert that the user make.
An example would make things much clearer :
+-------+----------------------------------+--------------+
| text | md5 | insert_times |
+-------+----------------------------------+---------+----+
| 123 | 202cb962ac59075b964b07152d234b70 | 834 |
| abc | 900150983cd24fb0d6963f7d28e17f72 | 923 |
+-------+----------------------------------+--------------+
where text is a primary key.
Whenever a user tries to inserts 123 (using INSERT IGNORE to not get error messages because 123 already exists), the insert_times would increase by 1, keeping track of number of tries of inserting an already existing text in the table.
Any ideas on how to make it?
I cannot make triggers because my db host (godaddy) doesn’t allow triggers.
If not possible to do in mysql, can I pull it off with the help of php?
To do this within a single, simple query, simply use the
ON DUPLICATE KEY UPDATEfunction within MySQL. With this you simply do your INSERT followed by what needs to be updated in the ‘duplicate’ row. For example:However, I personally prefer the PHP approach, as in most of my queries things usually aren’t as cut and dry as a simple update:
This method leaves for more room to do trigger other events/ functions as well, if needed.