PHP -SQL语句错误
我尝试使用我的网站数据库实现视图计数器。
我的代码:
函数,创建查询:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果有一个值重复数组中的最后一个值的值,则您的代码将无法正常工作,因为
$ value === end($ arrelement)
对重复是正确的。这将使您在查询中添加额外的值
。不要使用循环,请使用
umpode()
。那么,您无需特别治疗最后的迭代。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 extraVALUES
to the query.Don't use loops, use
implode()
. Then you don't need to treat the last iteration specially.那对您的代码的修改如何?
What about this modification to your code?