php - 将新变量定义为旧值加 1 的总和

标签 php mysql wordpress variables

我有一些插件代码,应该在数据库中查找“up_votes”的数量,如果单击了输入,则使用 UPDATE 查询向数据库添加另一个“up”投票。

问题是,它只能工作一次。我可以单击输入,并将值从 0 更新为 2,但在页面重新加载时无法更改为 3、4、5 等。显然,出了问题。

这是带有标记为“问题”的注释的代码,我遇到了麻烦:

/*
DISPLAY THE LIST
*/          
//Get the list from the database, and set the variables for display in the output.
$get_list = $wpdb->get_results('SELECT entry_ID, '.$cb_lud_field1_name.' AS "field1", '.$cb_lud_field2_name.' AS "field2", up_votes, down_votes, up_votes - down_votes AS "total_votes"
    FROM '.$cb_lud_table.'
    GROUP BY entry_ID
    ORDER BY total_votes DESC
    ',OBJECT);

//Check if list is null, and if so, set a variable to display a warning. Otherwise, display the list.
if (empty($get_list)) {
    $cb_lud_sc_output .= "<em>Warning: You don't seem to have any records for this list. Why don't you add some now?</em>";
    $cb_lud_sc_output .= $cb_lud_sc_form;
    return $cb_lud_sc_output;
}
else {
    $cb_lud_sc_output .= $cb_lud_sc_form;
    $cb_lud_sc_output .= '</br>';
    $cb_lud_sc_output .= '<table border="1" cellpadding="10">';
    $cb_lud_sc_output .= '<tr><td align="center"><strong>'.$cb_lud_field1_name.'</strong></td><td align="center"><strong>'.$cb_lud_field2_name.'</strong></td><td align="center"><strong>Score</strong></td><td align="center"><strong>Vote Up/Down</strong></td></tr>';
        foreach ($get_list as $list_items) {
            $cb_lud_sc_output .= '<tr><td>'.stripslashes($list_items->field1).'</td><td>'.stripslashes($list_items->field2).'</td><td>'.$list_items->total_votes.'</td><td><form action="" method="post"><input name="arrow-up-ID-'.$list_items->entry_ID.'" type="image" src="'.$cb_lud_upimg.'" value="'.$list_items->entry_ID.'"/>&nbsp;<input name="arrow-down-ID-'.$list_items->entry_ID.'" type="image" src="'.$cb_lud_downimg.'" value="'.$list_items->entry_ID.'"/></form></td></tr>';
        }
    $cb_lud_sc_output .= '</table>';
    //Problem --> Seems like I've set a variable for the current_up_votes, added it to 1, so if there are currently 2 up_votes, it should go to 3, but it stops at 2 for some reason.
    //find the values posted to the form
    $cb_lud_arrow_keys = array_keys($_POST);
    $cb_lud_arrow_values = array_values($_POST);
    $cb_lud_entry_id = (int)$cb_lud_arrow_values[2];
    $cb_lud_current_up_votes = $wpdb->query('SELECT up_votes
        FROM '.$cb_lud_table.' 
        WHERE entry_ID = '.$cb_lud_entry_id.'', ARRAY_A);
    $i = 1;
    $cb_lud_new_up_votes = $cb_lud_current_up_votes + $i;
    var_dump($cb_lud_current_up_votes);
    var_dump($cb_lud_base_votes);
    var_dump($cb_lud_new_up_votes);

        //set some variables to "up" or "down" depending on form input.
            if (strpos($cb_lud_arrow_keys[2], 'up-ID') > 0) {
                $cb_lud_arrow_direction = "up";
            }
            else if (strpos($cb_lud_arrow_keys[2], 'down-ID') > 0) {
                $cb_lud_arrow_direction = "down";
            }
            else {
                $cb_lud_arrow_direction = "nothing";
            }

        //create the update query that will record the up and down votes.
            if ($cb_lud_arrow_direction == "up" && $cb_lud_arrow_direction != "nothing") {
                $wpdb->query('UPDATE '.$cb_lud_table.' SET up_votes='.$cb_lud_new_up_votes.'
                    WHERE entry_ID='.$cb_lud_entry_id.'');
            }
            else if ($cb_lud_arrow_direction == "down" && $cb_lud_arrow_direction != "nothing") {
                //Get the id and update it to the down field
            }

    return $cb_lud_sc_output;
}

最佳答案

要测试增量,请尝试将 UPDATE 查询更改为:

$wpdb->query('UPDATE '.$cb_lud_table.' SET up_votes=up_votes+1
                WHERE entry_ID='.$cb_lud_entry_id.'');

关于php - 将新变量定义为旧值加 1 的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875525/

相关文章:

PHP Mysql 查询,没有错误,不会插入行

MySQL 多组通过使用

wordpress - 当通过页面规则强制使用 SSL "on"时,控制 Cloudflare 上的缓存和 CDN

php - 使用 LIKE 运算符优化 10k 记录的 mysql 查询

php - 带附件的电子邮件 Cron 作业

php - 重定向 PHP

mysql - 将两列分组,对另一列中的相应值进行计数,并将计数放入两列中

mysql - 如何格式化涉及除法的 MySQL 查询

php - Bitnami Wampstack : PHP caching requires server restart

wordpress - 使用 cron 作业运行 wp cli 命令