Below function works with ajax ... but I am getting a null result ... I want that if there is no data in the database then insert operation is performed and when there is data the update should work. Only update or only insert works but when I use both ... the answer is null ... currently my zvotes table has no data so insert operation should be performed. In update it searches the same value and try to replace it with a new value. I want the user to enter once and then if he presses button again he can replace his previous value. Thanks I am getting null right now.. I am just pasting the php code .. with this there is a simple ajax call to show and parse passed json data to a div.

PHP Code

add_action( 'wp_ajax_star', 'star' );
add_action( 'wp_ajax_nopriv_star', 'star');

function star()

    $user_info = wp_get_current_user();
    $passedvalue = $_POST['clickval'];
    $competition = $_POST['compete'];
    $aid = $_POST['foo'];
    global $wpdb;    
    //$sqll = "SELECT zvotes.zvotes FROM zvotes WHERE zvotes.votedby = '$user' && zvotes.zcompetition = '$competition'";

     $previous = $wpdb->get_var("SELECT zvotes.zvotes FROM zvotes WHERE zvotes.votedby = '$user' && zvotes.zcompetition = '$competition'");
    echo json_encode($previous);die();
    if (count($previous)>0){
    global $wpdb;    
    $reel = $wpdb->get_var("UPDATE zvotes SET zvotes.zvotes='$passedvalue' WHERE zvotes.votedby='$user' && zvotes.competition='$competition'");
    echo json_encode("hi i am blocking the insert operation");die();

    else if(count($previous<1))
                'zvotes' => $passedvalue,
                'zcompetition' => $competition,
                'aid' => $aid,
                'votedby' => $user
     $sqll = "SELECT Sum(zvotes.zvotes) AS votessum FROM zvotes WHERE zvotes.aid = '$aid' && zvotes.zcompetition = '$competition'";

     $reel = $wpdb->get_var($sqll) or die(mysql_error());
        echo json_encode ($reel);


  • please reply ... anybody – Zeeshan May 17 '15 at 13:40
  • Why are you using array count on the get_var result? This part count($previous<1)) will not work either. Also watch out for possible SQL injections. – birgire May 17 '15 at 15:03
  • You've got an early die() in there - I guess that's for debugging? Does the code still not work if you remove it? – TheDeadMedic May 17 '15 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.