我有一些插件代码应该在数据库中查找“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.'"/> <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;
}