-1

I am trying to register in my database, the words that the user searches on my website.

The idea is the following: If the searched word matches one already registered in my database, I simply add 1 to the number of times the word was searched. If the word is not found in my search table, I add a new word to my table.

The problem is what do I look for a word, if it is registered in my table, modify all the records with the name of the word you are looking for and increase the quantities by one. For example: I have two registers, word 1 and word 2, if I search for word 1 it returns, it updates the two words with the name of the word 1 and the quantities with the word 1

The fields of my table are the id, the word and the amount

Where is the problem in my code?

I share the sector where the call is made to the controller only, and then everything about the controller and the model

search.php

$response= SearchController::ctrNewSearch($word);

search.controller.php

<?php

class SearchController{

    public function ctrNewSearch($word){

        $table = "searchs";

        $response = SearchModel::mdlShowSearchs($table);

        $foundWord = 0;
        $amount= "1";

        foreach ($response as $key => $value) {

            if ($value["word"] == $word) {

                $foundSearch= 1;
                $id = $value["id"];
                $updatedAmount= $value["amount"] + 1;

            } 

        }

        if ($foundWord == 1){

            $response1= SearchModel::mdlUpdateSearch($table, $word, $id, $updatedAmount);
            return $response1;

        } else {

            $response0 = SearchModel::mdlAddSearch($table, $word, $amount);
            return $response0;

        }

    }

}

search.model.php

<?php

require_once "conection.php";

class SearchModel{

    static public function mdlShowSearchs($table){

        $stmt = Conexion::conectar()->prepare("SELECT * FROM $table");

        $stmt -> execute();

        return $stmt -> fetchAll();

        $stmt -> close();

        $tmt =null;

    }

    static public function mdlAddSearch($table, $word, $amount){

        $stmt = Conexion::conectar()->prepare("INSERT INTO $table (word, amount) VALUES (:word, :amount)");

        $stmt->bindParam(":word", $word, PDO::PARAM_STR);
        $stmt->bindParam(":amount", $amount, PDO::PARAM_INT);

        if($stmt->execute()){ 

            return "ok"; 

        }else{ 

            return "error"; 

        }

        $stmt->close();

        $tmt =null;
    }

    static public function mdlUpdateSearch($table, $word, $id, $updatedAmount){

        $stmt = Conexion::conectar()->prepare("UPDATE $table SET word = :word, amount = :amount WHERE $id = :id");

        $stmt->bindParam(":word", $word, PDO::PARAM_STR);
        $stmt->bindParam(":amount", $updatedAmount, PDO::PARAM_INT);
        $stmt->bindParam(":id", $id, PDO::PARAM_INT);

        if($stmt -> execute()){

            return "ok";

        }else{

            return "error"; 

        }

        $stmt -> close();

        $stmt = null;

    }

}
Marc Delisle
  • 8,879
  • 3
  • 29
  • 29
  • `if ($value["word"] = $word) `: it should be `==`. Also same with `if ($foundWord = 1)` –  Feb 01 '19 at 01:21
  • @catcon I made the changes you told me. Now the only problem is that if I search for a word that was already registered in my table, I change all the fields (even if they have different words) with the searched word and quantity, instead of changing only the word field. –  Feb 01 '19 at 01:30

1 Answers1

0

For each loop is having issue where if condition is not checking but its assigning which is always true. Update query should be in for loop and $foundword variable assign it to 0 every time, if there is word found. I made some changes try it out and try to resolve if there is syntax issue.

   public function ctrNewSearch($word){

    $table = "searchs";

    $response = SearchModel::mdlShowSearchs($table);

    $foundWord = 0;
    $amount= "1";

    foreach ($response as $key => $value) {

        if ($value["word"] == $word) {

            $foundWord = 1;
            $id = $value["id"];
            $updatedAmount= $value["amount"] + 1;

        }        

        if ($foundWord == 1){
          $response1= SearchModel::mdlUpdateSearch($table, $word, $id, $updatedAmount);
        $foundWord = 0;
        return $response1;

    } else {
        $response0 = SearchModel::mdlAddSearch($table, $word, $amount);
        return $response0;
    }
  }
 }
}
Pramod Patil
  • 2,704
  • 2
  • 14
  • 20
  • triple equal type coersion always check for it's type and value at the same time. So consider [this.](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Roshan Feb 01 '19 at 01:28
  • With == it works better, according to the comment of a user in my publication. Now the only problem is that if I search for a word that was already registered in my table, I change all the fields (even if they have different words) with the searched word and quantity, instead of changing only the word field. –  Feb 01 '19 at 01:32
  • Where is the select query to search for the word in database? – Pramod Patil Feb 01 '19 at 01:40
  • The word comes from search.php, where by means of the friendly routes, the word is in the last "/". So, I send it this way: $ word = $ routes[3]; If I do an echo of $ word, it always gives me the result of the word I am writing. So from the beginning the process is good. The problem is when the word is already registered in my database –  Feb 01 '19 at 01:53
  • Thanks for the update. I have reviewed it well, and I think that the error is in the syntax of mdlUpdateSearch, since I have reviewed with echo all the variables within the foreach when a word is already registered in the database, and it is all correct. The problem occurs when the UPDATE is done, I am updating all the records in the table with the searched word and the updated amount. But I do not find the error, maybe I can not see it –  Feb 01 '19 at 02:11
  • I could find the error. It was in the "WHERE" of the "UPDATE", change $ id to id. I will take your answer as correct since it was the one that led me to a correct change and later detection of the error –  Feb 01 '19 at 02:51