php mysqli更新函数
php function to mysqli update
正在尝试创建一个函数来更新任何 table 的任何行,但我在使用它时遇到了麻烦。
在数组中发送的数据,其中数组索引是 table 中的字段名称,值是该索引的新值。
例如:
$args["name"] = "NewName";
$args["city"] = "NewCity";
$args["id"] = 4; // row ID to update
我得到了什么:
function create_update_query($table, $keys){
$keys = array_map('escape_mysql_identifier', $keys);
$table = escape_mysql_identifier($table);
$updates = "";
$count = 0;
foreach($keys as $index => $value){
if($index != "id"){
$count++;
if($count == count($keys)-1){
$updates = $updates . "$index = ?";
}else{
$updates = $updates . "$index = ?,";
}
}
}
return "UPDATE $table SET $updates WHERE id = ? LIMIT 1";
}
之后,我就有了真正做查询的功能:
function crud_update($conn, $table, $data){
$sql = create_update_query($table, array_keys($data));
if(prepared_query($conn, $sql, array_values($data))){
$errors [] = "OK";
}else{
$errors [] = "Something weird happened...";
}
}
生成预处理语句本身的函数:
function prepared_query($mysqli, $sql, $params, $types = ""){
$types = $types ?: str_repeat("s", count($params));
if($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param($types, ...$params);
$stmt->execute();
return $stmt;
} else {
$error = $mysqli->errno . ' ' . $mysqli->error;
echo "<br/>".$error;
}
}
尝试提交符合以下条件的数据时:
$args['name'] = "Novo Nome";
$args['field'] = "New Field";
$args['numaro'] = 10101010;
$args['id'] = 4;
//create_update_query("teste_table", $args);
crud_update($link, "teste_table", $args);
出现错误:
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 = ?,2 = ?,3 = ? WHERE id = ? LIMIT 1' at line 1
但是,如果我回显 create_update_query
创建的查询,似乎没问题:
UPDATE `teste_table` SET name = ?,field = ?,numaro = ? WHERE id = ? LIMIT 1
如有任何帮助,我们将不胜感激。
谢谢。
问题是,当您将密钥传递给 create_update_query()
时,
create_update_query($table, array_keys($data));
使用 array_keys()
只会获取键名,因此 $keys
参数只是一个字段名称列表,类似于 ...
Array(
0=> 'name',
1 =>'field',
2 =>'numaro'
)
然后您使用
提取数据
foreach($keys as $index => $value){
并用
构建你的SQL
$updates = $updates . "$index = ?";
所以此时,索引是数值,所以将这些行更改为...
$updates = $updates . "$value = ?";
这是字段的名称。
随着其他各种变化,我建议代码应该是...
foreach($keys as $value){
if($value != "id"){
$updates = $updates . "$index = ?,";
}
}
$updates = rtrim($updates, ",");
return "UPDATE $table SET $updates WHERE id = ? LIMIT 1";
正在尝试创建一个函数来更新任何 table 的任何行,但我在使用它时遇到了麻烦。
在数组中发送的数据,其中数组索引是 table 中的字段名称,值是该索引的新值。
例如:
$args["name"] = "NewName";
$args["city"] = "NewCity";
$args["id"] = 4; // row ID to update
我得到了什么:
function create_update_query($table, $keys){
$keys = array_map('escape_mysql_identifier', $keys);
$table = escape_mysql_identifier($table);
$updates = "";
$count = 0;
foreach($keys as $index => $value){
if($index != "id"){
$count++;
if($count == count($keys)-1){
$updates = $updates . "$index = ?";
}else{
$updates = $updates . "$index = ?,";
}
}
}
return "UPDATE $table SET $updates WHERE id = ? LIMIT 1";
}
之后,我就有了真正做查询的功能:
function crud_update($conn, $table, $data){
$sql = create_update_query($table, array_keys($data));
if(prepared_query($conn, $sql, array_values($data))){
$errors [] = "OK";
}else{
$errors [] = "Something weird happened...";
}
}
生成预处理语句本身的函数:
function prepared_query($mysqli, $sql, $params, $types = ""){
$types = $types ?: str_repeat("s", count($params));
if($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param($types, ...$params);
$stmt->execute();
return $stmt;
} else {
$error = $mysqli->errno . ' ' . $mysqli->error;
echo "<br/>".$error;
}
}
尝试提交符合以下条件的数据时:
$args['name'] = "Novo Nome";
$args['field'] = "New Field";
$args['numaro'] = 10101010;
$args['id'] = 4;
//create_update_query("teste_table", $args);
crud_update($link, "teste_table", $args);
出现错误:
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 = ?,2 = ?,3 = ? WHERE id = ? LIMIT 1' at line 1
但是,如果我回显 create_update_query
创建的查询,似乎没问题:
UPDATE `teste_table` SET name = ?,field = ?,numaro = ? WHERE id = ? LIMIT 1
如有任何帮助,我们将不胜感激。 谢谢。
问题是,当您将密钥传递给 create_update_query()
时,
create_update_query($table, array_keys($data));
使用 array_keys()
只会获取键名,因此 $keys
参数只是一个字段名称列表,类似于 ...
Array(
0=> 'name',
1 =>'field',
2 =>'numaro'
)
然后您使用
提取数据foreach($keys as $index => $value){
并用
构建你的SQL$updates = $updates . "$index = ?";
所以此时,索引是数值,所以将这些行更改为...
$updates = $updates . "$value = ?";
这是字段的名称。
随着其他各种变化,我建议代码应该是...
foreach($keys as $value){
if($value != "id"){
$updates = $updates . "$index = ?,";
}
}
$updates = rtrim($updates, ",");
return "UPDATE $table SET $updates WHERE id = ? LIMIT 1";