PHP -SQL语句错误

发布于 2025-02-11 09:44:28 字数 1972 浏览 2 评论 0原文

我尝试使用我的网站数据库实现视图计数器。

我的代码:

函数,创建查询:

public function addRowViewCompany($arrElement) {
        $query = "INSERT INTO views_company (";
        $columns = "";
        $values = "";

        // columns
        foreach ($arrElement as $column => $value) {

            if ($value === end($arrElement)) {
                // if last column
                $columns .= $column . ") VALUES(";
                $values .= ":" . $column . ");";
            } else {
                $columns .= $column . ", ";
                $values .= ":" . $column . ", ";
            }
        }

        $this->setQuery($query . $columns . $values);
        $this->prepareStatement();
        $this->executeStatement($arrElement);
    } 

调用方法:

            $database->addRowViewCompany(array(
                "view_id" => null,
                "view_date" => date("Y-m-d"),
                "company_id" => intval($companyID),
                "views" => 1
            ));

创建的查询:查询

INSERT INTO views_company (view_id, view_date, company_id) VALUES(views) VALUES(:view_id, :view_date, :company_id);:views);

在此位置是错误的:插入tovie_company(view_id,view_date,company_id)values(view> )valut(:view_id,view_id, :view_date,:company_id );:视图);

但是我不明白为什么它会被创建 对于其他用例,此功能有效。

编辑:

public function __construct() {
    $this->connection = new PDO("mysql:host=localhost;dbname=ref;charset=utf8", "***", "***");
}

public function setQuery(string $query) {
    $this->query = $query;
}

public function prepareStatement() {
    $this->statement = $this->connection->prepare($this->query);
}

public function executeStatement(array $args = null) {
    if ($args == null) {
        $this->statement->execute();
    } else {
        $this->statement->execute($args);
    }
} 

I try to implement a view counter with database for my website.

My code:

Function, that creates the query:

public function addRowViewCompany($arrElement) {
        $query = "INSERT INTO views_company (";
        $columns = "";
        $values = "";

        // columns
        foreach ($arrElement as $column => $value) {

            if ($value === end($arrElement)) {
                // if last column
                $columns .= $column . ") VALUES(";
                $values .= ":" . $column . ");";
            } else {
                $columns .= $column . ", ";
                $values .= ":" . $column . ", ";
            }
        }

        $this->setQuery($query . $columns . $values);
        $this->prepareStatement();
        $this->executeStatement($arrElement);
    } 

calling the method:

            $database->addRowViewCompany(array(
                "view_id" => null,
                "view_date" => date("Y-m-d"),
                "company_id" => intval($companyID),
                "views" => 1
            ));

The query that gets created:

INSERT INTO views_company (view_id, view_date, company_id) VALUES(views) VALUES(:view_id, :view_date, :company_id);:views);

The Query is wrong at this location: INSERT INTO views_company (view_id, view_date, company_id) VALUES(views ) VALUES(:view_id, :view_date, :company_id );: views);

But I don't get why it gets created wrong
For other use cases this function works.

EDIT:

public function __construct() {
    $this->connection = new PDO("mysql:host=localhost;dbname=ref;charset=utf8", "***", "***");
}

public function setQuery(string $query) {
    $this->query = $query;
}

public function prepareStatement() {
    $this->statement = $this->connection->prepare($this->query);
}

public function executeStatement(array $args = null) {
    if ($args == null) {
        $this->statement->execute();
    } else {
        $this->statement->execute($args);
    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2025-02-18 09:44:28

如果有一个值重复数组中的最后一个值的值,则您的代码将无法正常工作,因为$ value === end($ arrelement)对重复是正确的。这将使您在查询中添加额外的

不要使用循环,请使用umpode()。那么,您无需特别治疗最后的迭代。

$columns = implode(', ', array_keys($arrElement);
$values = implode(', ', array_map(function($col) { return ':' . $col; }, array_keys($arrElement)));

$this->setQuery("$query ($columns) VALUES ($values)");

Your code won't work if there's a value that duplicates the last value in the array, because $value === end($arrElement) will be true for the duplicate. That will cause you to add an extra VALUES to the query.

Don't use loops, use implode(). Then you don't need to treat the last iteration specially.

$columns = implode(', ', array_keys($arrElement);
$values = implode(', ', array_map(function($col) { return ':' . $col; }, array_keys($arrElement)));

$this->setQuery("$query ($columns) VALUES ($values)");
日暮斜阳 2025-02-18 09:44:28

那对您的代码的修改如何?

public function addRowViewCompany($arrElement) {
    $query = "INSERT INTO views_company (";
    $columns = "";
    $middle = ") VALUES(";
    $values = "";
    $end = ")";

    // columns
    foreach ($arrElement as $column => $value) {
        $columns .= ((strlen($column)==0)?"":", ") . $column;
        $values .= ((strlen($values)==0)?"":", ") . ":" . $column;
    }

    $this->setQuery($query . $columns . $middle . $values . $end);
    $this->prepareStatement();
    $this->executeStatement($arrElement);
} 

What about this modification to your code?

public function addRowViewCompany($arrElement) {
    $query = "INSERT INTO views_company (";
    $columns = "";
    $middle = ") VALUES(";
    $values = "";
    $end = ")";

    // columns
    foreach ($arrElement as $column => $value) {
        $columns .= ((strlen($column)==0)?"":", ") . $column;
        $values .= ((strlen($values)==0)?"":", ") . ":" . $column;
    }

    $this->setQuery($query . $columns . $middle . $values . $end);
    $this->prepareStatement();
    $this->executeStatement($arrElement);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文