我对“未知”专栏有疑问。 这是我在萤火虫中找回的错误。
错误:SQLSTATE [42S22]:未找到列:1054“where子句”中的未知列“admin”UPDATE用户SET session_key = 1234567890 WHERE username ='admin'
这是参数和调用语句
$ table ='users';
$ fields_vals = array('session_key'=> $ session_key);
$ where = array('username'=> $ username);
$ dbResult = $ this-> DB-> sql_update($ fields_vals,$ table,$ where);
/*
* UPDATE
* $data -> assoc array containing (field => value) to be UPDATED.
* $where -> Where clause (only a single argument)
* $table -> to be updated
*/
public function sql_update($fieldsVals, $table, $where)
{
try {
//Values to be updated in in a assoc array
//Extract values and fields and concatenate with '=' ( field = value )
$upd_string = '';
foreach($fieldsVals as $name => $value){
$upd_string .= $name .' = :'. $name .' ,';
}
//Trim last comma that was appended
$upd_string = rtrim($upd_string, ',');
// Formulate the where clause
$where_str = '';
foreach($where as $wName => $wValue){
$where_str .= "$wName = $wValue";
}
//Set Query
//$query = "UPDATE {$table} SET {$upd_string} WHERE $where_str";
// THIS IS WHERE I EXPLICITLY RAN THE QUERY, BUT GOT EXACTLY THE SAME ERROR.
$query = "UPDATE users SET session_key = 1234567890 WHERE username = 'admin'";
$stmt = $this->conn->prepare($query);
//Exec
foreach($fieldsVals as $k => &$v){
$stmt->bindParam(":{$k}", $v);
}
$stmt->execute();
return true;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
return false;
}
}
以下是该列确实存在的证据。
请注意 - >其中username ='admin'
答案 0 :(得分:2)
尝试将WHERE
属性作为准备好的变量传递:
$query = "UPDATE users SET session_key = 1234567890 WHERE username = :username";
$fieldsVals[":username"] = "admin";
答案 1 :(得分:1)
$where_str .= "$wName = '$wValue'";
否则,您的查询将显示为" WHERE username = admin"
答案 2 :(得分:1)
正确的用法是:
$stmt = $this->conn->prepare('UPDATE users SET session_key = :session WHERE username = :username');
$stmt->bindParam(':session', session_id(), PDO::PARAM_STR);
$stmt->bindParam(':username', $username, PDO::PARAM_STR);
然后执行。
我认为你的变量$fieldsVals
是错误的,因为你做的是foreach。也许你偶然切换键和val,导致WHERE admin=username
。
更新
@MarcinNabiałek当然你错过了引号。
但似乎错误来自脚本的不同部分。
答案 3 :(得分:1)
您应该如何为整个查询使用预准备语句,因此您应该将当前函数更改为:
function sql_update($fieldsVals, $table, $where)
{
//Values to be updated in in a assoc array
//Extract values and fields and concatenate with '=' ( field = value )
$upd_string = '';
foreach ($fieldsVals as $name => $value) {
$upd_string .= $name . ' = :set_' . $name . ' ,';
}
//Trim last comma that was appended
$upd_string = rtrim($upd_string, ',');
// Formulate the where clause
$where_str = '';
foreach ($where as $wName => $wValue) {
$where_str .= $wName . ' = :wh_' . $wName . ' ,';
}
//Trim last comma that was appended
$where_str = rtrim($where_str, ',');
//Set Query
$query = "UPDATE {$table} SET {$upd_string} WHERE $where_str";
$stmt = $this->conn->prepare($query);
//Exec
foreach ($fieldsVals as $k => &$v) {
$stmt->bindParam(":set_{$k}", $v);
}
foreach ($where as $k => &$v) {
$stmt->bindParam(":wh_{$k}", $v);
}
$stmt->execute();
return true;
}
答案 4 :(得分:0)
您的查询似乎没有在PHP中使用'
。您要么不使用它,要么使用看起来相似的其他符号。
如果我在phpmyAdmin查询中运行:
UPDATE users SET session_key = 1234567890 WHERE username = admin
我也收到错误消息:
#1054 - Unknown column 'admin' in 'where clause'
但是当我有
时UPDATE users SET session_key = 1234567890 WHERE username = 'admin'
一切正常
在您的代码中,您应该明确更改:
$where_str .= "$wName = $wValue";
到
$where_str .= "$wName = '$wValue'";
答案 5 :(得分:0)
session_key
是varchar
您必须使用如下所示的查询
$query = "UPDATE users SET session_key = '1234567890' WHERE username = 'admin'";
答案 6 :(得分:-4)
列admin
不存在!虽然列administrator
存在。
我在您提供的屏幕截图中的mysql表中没有看到任何管理列。
您只需将mysql表中administrator
列的名称更改为admin
即可,一切正常。