# Increment Same Keyword Entry +1 each time it is searched.

**URL:** <https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337>\
**Category:** MySQL database\
**Created:** [April 12, 2021, 5:38pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337 "2021-04-12T17:38:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![kclavezz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kclavezz](https://www.phphelp.com/u/kclavezz)\
**Post date:** [April 12, 2021, 5:38pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/1 "2021-04-12T17:38:45Z")

</div>

Hello, I have a webpage with an HTML form that sends a keyword that is searched to an SQL database that I have. I have a column named Search\_attempts in the database that I am trying to have increment if the same keyword is entered in the HTML form. An example would be if the word Dog was entered 2 times, Instead of there being the word dog in the keyword column 2 times, I would want it in there 1 time, and have the Search\_attempts column have the number 2.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [April 12, 2021, 5:45pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/2 "2021-04-12T17:45:36Z")

</div>

Do you mean you want to remove duplicates in the search text or do you want to save a counter when a value is searched for?

If you want to remove dups in the input text, that is fairly easy. Saving a counter for every search input can get complicated. Is all of your searches just one word? If so, you can keep an array of the searches made and keep a counter for each. But, I am curious why you would need to do that.

If all your searches are just one word each, you can create a “searches” table with “word” and “count” and just increment them as needed. Perhaps you can explain a little more on what you are trying to do.

---

<div class="post-metadata">

**Author:** ![kclavezz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kclavezz](https://www.phphelp.com/u/kclavezz)\
**Post date:** [April 12, 2021, 6:04pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/3 "2021-04-12T18:04:12Z")

</div>

Ya, I can try and explain some more. So the PHP script has an HTML form that sends a word entered by a user over to an SQL database that I have created. Inside that database there is a table with a number column that is the primary key which acts as which number keyword that is. the next column is Keyword. that is the word that the user entered. The final column in the table is named Search\_attempts. That is the column that I am trying to get to work. I want it to show the number of times that Keyword was entered. Say the word Dog was entered 3 times, and Cat entered 2 times. I would want the search\_attempts column to show 3 and 2 in the Dog and Cat row.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [April 12, 2021, 6:23pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/4 "2021-04-12T18:23:25Z")

</div>

Okay, first, you should TRIM() the inputs as users might add extra spaces. Also, in your example, you should also check to make sure only one word is entered and no special characters so your database can not be corrupted.

Next, you would just have to run an UPDATE query based on the word entered. Therefore, you can use a query loosely something like this:

```auto
UPDATE table-name 
  SET Search_attempts = Search_attempts + 1 
  WHERE Keyword = $input_keyword

```

Should work… Good luck

---

<div class="post-metadata">

**Author:** ![kclavezz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kclavezz](https://www.phphelp.com/u/kclavezz)\
**Post date:** [April 12, 2021, 6:41pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/6 "2021-04-12T18:41:02Z")

</div>

$sql1 = “INSERT into knktable (Keyword) VALUES (?)”;  
That is the part of my php scripts that sends it to the SQL database. so would the WHERE statement have $sql1 for the $input\_keyword?

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [April 12, 2021, 6:57pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/7 "2021-04-12T18:57:26Z")

</div>

You can use [`ON DUPLICATE KEY UPDATE`](https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html) plus some extra indexes on your table.

For this example, I’ve created a table as follows:

```sql
CREATE TABLE search_history (
    id INT AUTO_INCREMENT PRIMARY KEY,
    keyword VARCHAR(255) UNIQUE,
    num_of_searches INT DEFAULT 1
);

```

Note the `UNIQUE` index on the `keyword` column.

Now you can run the following:

```sql
INSERT INTO search_history(keyword)
    VALUES ("dog")
    ON DUPLICATE KEY UPDATE num_of_searches = num_of_searches + 1;

```

The first time you run this query, your table will look like this:

```auto
+----+---------+-----------------+
| id | keyword | num_of_searches |
+----+---------+-----------------+
| 1 | dog | 1 |
+----+---------+-----------------+

```

Now if you run your `insert` query again, your table will look like this:

```auto
+----+---------+-----------------+
| id | keyword | num_of_searches |
+----+---------+-----------------+
| 1 | dog | 2 |
+----+---------+-----------------+

```

---

<div class="post-metadata">

**Author:** ![kclavezz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kclavezz](https://www.phphelp.com/u/kclavezz)\
**Post date:** [April 12, 2021, 7:17pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/8 "2021-04-12T19:17:47Z")

</div>

I just tried that and I understand how that should work, but it is not working with my table. Is there any way that you would be able to meet for a screen share? it should not take more than say 15 or 20 mins I would say.

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [April 12, 2021, 8:55pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/9 "2021-04-12T20:55:29Z")

</div>

I’m on and off my PC. What does your table look like? Run the query `describe Search_attempts` and show us that along with the query you’re using to insert.

---

<div class="post-metadata">

**Author:** ![kclavezz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kclavezz](https://www.phphelp.com/u/kclavezz)\
**Post date:** [April 12, 2021, 9:04pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/10 "2021-04-12T21:04:35Z")

</div>

I ran that code that you sent me which is good for only using the SQL database. it did increment which was cool. The problem was that when I used the HTML form to send the same word again I was receiving an error for the same keyword entry. This is because I changed the Keyword column to unique I believe.

---

<div class="post-metadata">

**Author:** ![kclavezz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kclavezz](https://www.phphelp.com/u/kclavezz)\
**Post date:** [April 12, 2021, 9:07pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/11 "2021-04-12T21:07:31Z")

</div>

![image](https://www.phphelp.com/uploads/default/original/2X/2/2dca585e9395673ea91cde658f22e27823869351.png)

---

<div class="post-metadata">

**Author:** ![kclavezz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kclavezz](https://www.phphelp.com/u/kclavezz)\
**Post date:** [April 12, 2021, 9:08pm UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/12 "2021-04-12T21:08:06Z")

</div>

Here is the php code that sends the keyword entered to the DB

 ![image](https://www.phphelp.com/uploads/default/original/2X/7/749aa1e2096f78d28e6e6a66986c489f2b29427a.png)

---

<div class="post-metadata">

**Author:** ![skawid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/bbe5ce/32.png) [@skawid](https://www.phphelp.com/u/skawid)\
**Post date:** [April 13, 2021, 6:57am UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/13 "2021-04-13T06:57:42Z")

</div>

What error are you seeing?

---

<div class="post-metadata">

**Author:** ![phdr](https://www.phphelp.com/letter_avatar_proxy/v4/letter/p/91b2a8/32.png) [@phdr](https://www.phphelp.com/u/phdr)\
**Post date:** [April 13, 2021, 9:46am UTC](https://www.phphelp.com/t/increment-same-keyword-entry-1-each-time-it-is-searched/33337/14 "2021-04-13T09:46:24Z")

</div>

You need to use the INSERT … ON DUPLICATE KEY UPDATE … query in your php code.
