gpt4 book ai didi

php - sql 和 php - 此查询或代码是否存在 ant 问题

转载 作者:搜寻专家 更新时间:2023-10-30 23:39:12 24 4
gpt4 key购买 nike

大家好,这是我的更新函数,它接受一些参数并计算它们是否是整数或字符串,然后处理值并抛出查询语句...

public function update ($table, $cols, $values, $addition) {
$db = $this->connect();
$i = 0;
$update = '';
if ((is_array($cols)) && (is_array($values))) {
foreach ($cols as $a) {
if (!is_int($values[$i])) {
$update = $a.'="'.$values[$i].'",';
} else {
$update = $a.'='.$values[$i].',';
}
$i++;
}
$update = substr($update, 0, -1);
} else {
if (!is_int($values)) {
$update = $cols.'="'.$values.'",';
} else {
$update = $cols.'='.$values.',';
}
}
echo "update ".$table." set ".$update." ".$addition."<br>";
try {
$sql = $db->prepare("update ".$table." set ".$update." ".$addition);
$sql->execute();
} catch (PDOException $e) {
print $e->getMessage();
}
$db = null;
}

这里是参数和sql查询

$this->db->update("brands", "car_count", $brandCarCount[0]+1, "where brand = '".$brand."'");

update brands set car_count=2, where brand = 'alfa_romeo'

我真的不明白发生了什么,我找不到问题所在。它适用于其他插入选择或删除功能。

我需要帮助!

最佳答案

这是删除尾随逗号的相关行:

$update = rtrim($update,',');

这是完整的代码:

public function update ($table, $cols, $values, $addition) {
$db = $this->connect();
$i = 0;
$update = '';
if ((is_array($cols)) && (is_array($values))) {
foreach ($cols as $a) {
if (!is_int($values[$i])) {
$update = $a.'="'.$values[$i].'",';
} else {
$update = $a.'='.$values[$i].',';
}
$i++;
}
} else {
if (!is_int($values)) {
$update = $cols.'="'.$values.'",';
} else {
$update = $cols.'='.$values.',';
}
}
$update = rtrim($update,',');
echo "update ".$table." set ".$update." ".$addition."<br>";
try {
$sql = $db->prepare("update ".$table." set ".$update." ".$addition);
$sql->execute();
} catch (PDOException $e) {
print $e->getMessage();
}
$db = null;
}

关于php - sql 和 php - 此查询或代码是否存在 ant 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37242946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com