我的删除条目代码是这样的,但它没有做任何事情
HTML
<form id="form3" name="form3" method="post" onsubmit="return validateForm();" action="">
Id <input type="text" class="txt" name="id" /><br />
<input type="submit" id="delete" value="delete"/>
</form>
PHP
global $wpdb;
if ( isset ( $_POST['id'] ) && ! empty ( $_POST['id'] ))
{
$wpdb->query("DELETE " . PRO_TABLE_PREFIX . "tutorial WHERE id='{$_POST['id']}'");
}
最佳答案
为了避免这样的混淆,我总是在需要连接字符串的地方使用sprintf()
更改:
global $wpdb;
if ( isset ( $_POST['id'] ) && ! empty ( $_POST['id'] )) {
$wpdb->query("DELETE " . PRO_TABLE_PREFIX . "tutorial WHERE id='{$_POST['id']}'");
}
至:
global $wpdb;
if ( isset ( $_POST['id'] ) )) {
$wpdb->query(sprintf("DELETE %stutorial WHERE id='%s'", PRO_TABLE_PREFIX, $_POST['id']));
}
需要注意的几点:
1) 您容易受到 SQL 注入(inject)
2) 使用 isset()
确定 $_POST['id']
的键是否实际上不是 NULL
后,您不需要通过 empty()
更新
您应该真正测试 $_POST['id']
是否有效。我建议您实现一个函数,例如 is_id_valid()
function is_id_valid(&$id){ //<-- IMPORTANT, Argument should be a reference
if ( ! isset($id) ){
return false;
}
if ( empty($id) ){
return false;
}
// add this if you expect $id to be a numeric value
// otherwise just ignore - do not add
if ( ! is_numeric($id) ){
return false;
}
//it's also a good to validate the length
if ( strlen($id) > ... && strlen($id) < ... ){
return false;
}
//seems like all tests passed
return true;
}
然后你会使用它,比如
if ( is_id_valid($_POST['id']) !== false ){
....
}
警告:它仍然容易受到 SQL 注入(inject)攻击
关于php - 我想删除 php mysql 中的 where id=$POST ['id' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14108583/