Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将新变量定义为旧值加1的和_Php_Mysql_Wordpress_Variables - Fatal编程技术网

Php 将新变量定义为旧值加1的和

Php 将新变量定义为旧值加1的和,php,mysql,wordpress,variables,Php,Mysql,Wordpress,Variables,我有一些插件代码,可以在数据库中查找up_投票数,如果单击了输入,则使用更新查询向数据库添加另一个up投票 问题是,它只工作一次。我可以单击输入,并将值从0更新为2,但在页面重新加载时无法转到3、4、5等。显然,出了点问题 下面是我遇到问题的代码,注释标记为问题: /* DISPLAY THE LIST */ //Get the list from the database, and set the variables for display in the output. $

我有一些插件代码,可以在数据库中查找up_投票数,如果单击了输入,则使用更新查询向数据库添加另一个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;
}

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

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

明亮的这么简单。谢谢你的帮助!