Php 在我的数据库中未正确注册搜索的单词

Php 在我的数据库中未正确注册搜索的单词,php,mysql,database,Php,Mysql,Database,我试图在我的数据库中注册用户在我的网站上搜索的单词 其思想如下:如果搜索的单词与数据库中已注册的单词匹配,我只需在单词搜索次数上加1即可。 如果在我的搜索表中找不到该单词,我将向表中添加一个新词 问题是我要查找什么单词,如果它已在我的表中注册,请使用您要查找的单词名称修改所有记录,并将数量增加一个。例如:我有两个寄存器,字1和字2,如果我搜索它返回的字1,它会用字1的名称更新两个字,用字1更新数量 我的表的字段是id、单词和金额 我的代码中的问题在哪里 我共享一个扇区,其中只对控制器进行调用,然

我试图在我的数据库中注册用户在我的网站上搜索的单词

其思想如下:如果搜索的单词与数据库中已注册的单词匹配,我只需在单词搜索次数上加1即可。 如果在我的搜索表中找不到该单词,我将向表中添加一个新词

问题是我要查找什么单词,如果它已在我的表中注册,请使用您要查找的单词名称修改所有记录,并将数量增加一个。例如:我有两个寄存器,字1和字2,如果我搜索它返回的字1,它会用字1的名称更新两个字,用字1更新数量

我的表的字段是id、单词和金额

我的代码中的问题在哪里

我共享一个扇区,其中只对控制器进行调用,然后是关于控制器和模型的所有内容

search.php

$response= SearchController::ctrNewSearch($word);
<?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;

        }

    }

}
<?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;

    }

}
search.controller.php

$response= SearchController::ctrNewSearch($word);
<?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;

        }

    }

}
<?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;

    }

}

每个循环都有一个问题,即if条件未检查,但其赋值始终为true。更新查询应该在for循环中,并且$foundword变量每次都将其分配给0(如果找到word)。我做了一些修改,尝试一下,并试图解决是否有语法问题

   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;
    }
  }
 }
}

如果($value[“word”]=$word)
:它应该是
=
。如果($foundWord=1)
@catcon我做了你告诉我的更改,那么
也一样。现在唯一的问题是,如果我搜索一个已经在我的表中注册的单词,我会用搜索的单词和数量更改所有字段(即使它们有不同的单词),而不是只更改单词字段。triple equal type coersion始终同时检查它的类型和值。因此,根据我的出版物中的用户评论,用它=工作得更好。现在唯一的问题是,如果我搜索已经在我的表中注册的单词,我会用搜索的单词和数量更改所有字段(即使它们有不同的单词),而不是只更改单词字段。在数据库中搜索单词的select查询在哪里?单词来自search.php,通过友好路线,单词位于最后一个“/”。所以,我这样发送:$word=$routes[3];如果我对$word进行回显,它总会给出我所写单词的结果。因此,从一开始,这个过程就是好的。问题是这个词已经在我的数据库中注册了,谢谢你的更新。我已经很好地回顾了它,我认为错误在mdlUpdateSearch的语法中,因为当一个单词已经在数据库中注册时,我已经用echo回顾了foreach中的所有变量,并且都是正确的。当更新完成时,问题就出现了,我正在用搜索的单词和更新的数量更新表中的所有记录。但是我没有发现错误,也许我看不到