所以,我有一个以下的js和php:
JS:
var names = jQuery('#name').val();
data : {'action':'AJAX' , name:names },
#name
值为"mike,sean,steve"
PHP:
global $wpdb;
$names = $_POST['name'];
$table = $wpdb->prefix . 'my_name';
$RSS_UPDATE = $wpdb->get_col("SELECT update_number FROM $table WHERE id_name IN ($names)");
//update_number are int (example: 0,3,1,2)
$name = explode(',', $names);
if ( $RSS_UPDATE ){
foreach ( $RSS_UPDATE as $RSS_SINGLE ){
$RSS_ROW_NEW = $RSS_SINGLE + 1;
$wpdb->update($table, array('update_number' => $RSS_ROW_NEW),array( 'id_name' => $name));
}
}
所以,很少有事情:
我想要实现的目标:
使用输入值,获取相应的update_number
。然后通过" 1"增加每个值。并使用新值更新同一列。
错误
Unknown column 'Array' in 'where clause' for query SELECT update_number FROM wp_my_name WHERE id_name IN (Array)
总的来说,有些事情是不对的......
有人能帮助我吗?
谢谢。
修改
这看起来不错吗?
if(!empty($_POST['name'])) {
$names = $_POST['name']; //array
$table = $wpdb->prefix . 'rh_subs';
$query = "SELECT update_number FROM $table WHERE id_name = %s";
$RSS_UPDATE = $wpdb->get_results($wpdb->prepare($query, $names));
if(!empty($RSS_UPDATE)) {
foreach($RSS_UPDATE as $RSS_SINGLE) { // for each row
$RSS_ROW_NEW = $RSS_SINGLE->update_number + 1;
$wpdb->update($table, array('update_number' => $RSS_ROW_NEW),array('id_name' => $RSS_SINGLE->id_name));
}
}
}
答案 0 :(得分:1)
首先,似乎$ _POST [' name']会返回一个数组。
您可以通过以下方式查看$ _POST ['名称']的确切内容:
var_dump($_POST['name'], true);
同样对于id_name,如果他们就像这些"迈克,肖恩,史蒂夫"然后你应该这样做,为字符串和逃避问题添加引号,以便它们可以像这样"迈克',' sean'史蒂夫' ;"当你使用WHERE IN
条款时:
$names = $_POST['name'];
if(!is_array($names)) $names = explode(",",$names);
$new_names = array();
foreach($names as $name){
$name = get_magic_quotes_gpc() ? stripslashes($name) : $name;
$new_names[] = "'".mysql_real_escape_string($name)."'";
}
$names = implode(",", $new_names);
答案 1 :(得分:1)
正如我在您之前发表的帖子中所说的那样,因为您需要使用多个输入,所以您需要使用WHERE IN
子句。
简单的例子是这样的:
$_POST['name']; // these are comma delimited string of names
// "mike,sean,steve"
所以从本质上讲,你需要在WHERE IN
子句中构造它们,如下所示:
WHERE id_name IN ('mike', 'sean', 'steve')
不安全和最肮脏的方式就是爆炸 - 在字符串上加上引号 - 再次用逗号再次将其破坏:
$names = array_map(function($e){
return "'$e'";
}, explode(',', $test));
$names = implode(',', $names);
// 'mike','sean','steve' // SATISFIES WHERE IN CLAUSE
// BUT UNSAFE!
因此,为了安全地执行此操作,请使用wpdb prepared语句。 (这可以让你开始)。
if(!empty($_POST['name'])) {
$names = explode(',', $_POST['name']); // explode the comma delimited string into an array
$table = $wpdb->prefix . 'my_name';
$stringPlaceholders = implode(', ', array_fill(0, count($names), '%s')); // create placeholders for the query statement, this will generate
$statement = $wpdb->prepare("SELECT update_number, id_name FROM $table WHERE id_name IN ($stringPlaceholders)", $names); // create the statement using those placeholders
$RSS_UPDATE = $wpdb->get_results($statement); // execute
// fetch resuls
if(!empty($RSS_UPDATE)) {
foreach($RSS_UPDATE as $RSS_SINGLE) { // for each row
$RSS_ROW_NEW = $RSS_SINGLE->update_number + 1;
$wpdb->update($table, array('update_number' => $RSS_ROW_NEW),array('id_name' => $RSS_SINGLE->id_name));
}
}
}
注意:当然,您可以自己创作。我认为你可以结合UPDATE
和WHERE IN
子句,这样你就可以执行所有这一次。