2013-05-11 2 views
-3

어떤 사람은 나를 도와주세요 수 있지만도움이 날

경고와 같은 오류를 얻을 위해 :) (mysqli_fetch_assoc 매개 변수 1,/응용 프로그램/MAMP는/htdocs에/라이브/vote_process 주어진 부울 mysqli_result 될 것으로 기대하고있다. php on line 45

이 오류는 콘텐츠 나 기사가 마음에 들면 (엄지 손가락으로) 데이터베이스 테이블의 레코드가 업데이트되지 않을 때 나타납니다. 그러나 콘텐츠 나 기사를 싫어할 때 오류가 나타나지 않습니다.

싫어요 (엄지 손가락 내려 가기) 기록은 싫어요했을 때 페이지에 표시되지만 페이지를 새로 고침하면 0을 표시합니다. 이 문제를 해결하는 더 좋은 방법이 있습니까?

PHP 코드

<?php 
if ($_POST) { 
    ### connect to mySql 
    $sql_con = mysqli_connect($db_host, $db_username, $db_password, $db_name) or die('could not connect to database'); 

    //get type of vote from client 
    $user_vote_type = trim($_POST["vote"]); 

    //get unique content ID and sanitize it (cos we never know). 
    $music_id  = filter_var(trim($_POST["music_id"]), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH); 

    //Convert content ID to MD5 hash (optional) 
    $music_id  = hash('md5', $music_id); 

    //check if its an ajax request, exit if not 
    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { 
     die(); 
    } 

    switch ($user_vote_type) { 

     ##### User liked the content ######### 
     case 'up': 
      //check if user has already voted, determined by unique content cookie 
      if (isset($_COOKIE["voted_" . $music_id])) { 
       header('HTTP/1.1 500 Already Voted'); //cookie found, user has already voted 
       exit(); //exit script 
      } 
      //get vote_up value from db using music_id 
      $qur   = mysqli_query($sql_con, "SELECT like FROM music_like WHERE music_id='$music_id' LIMIT 1"); 
      $get_total_rows = mysqli_fetch_assoc($qur); 
      if ($get_total_rows) { 
       //found record, update vote_up the value 
       mysqli_query($sql_con, "UPDATE music_like SET like=like+1 WHERE music_id='$music_id'"); 
      } else { 
       //no record found, insert new record in db 
       mysqli_query($sql_con, "INSERT INTO music_like (music_id, like) value('$music_id',1)"); 
      } 
      setcookie("voted_" . $music_id, 1, time() + 7200); // set cookie that expires in 2 hour "time()+7200". 
      echo ($get_total_rows["like"] + 1); //display total liked votes 
      break; 

     ##### User disliked the content ######### 
     case 'down': 
      //check if user has already voted, determined by unique content cookie 
      if (isset($_COOKIE["voted_" . $music_id])) { 
       header('HTTP/1.1 500 Already Voted this Content!'); //cookie found, user has already voted 
       exit(); //exit script 
      } 
      //get vote_up value from db using unique_content_id 
      $qur   = mysqli_query($sql_con, "SELECT dislike FROM music_like WHERE music_id='$music_id' LIMIT 1"); 
      $get_total_rows = mysqli_fetch_assoc($qur); 
      if ($get_total_rows) { 
       //found record, update vote_down the value 
       mysqli_query($sql_con, "UPDATE music_like SET dislike=dislike+1 WHERE music_id='$music_id'"); 
      } else { 
       //no record found, insert new record in db 
       mysqli_query($sql_con, "INSERT INTO music_like (music_id, dislike) value('$music_id',1)"); 
      } 
      setcookie("voted_" . $music_id, 1, time() + 7200); // set cookie that expires in 2 hour "time()+7200". 
      echo ($get_total_rows["dislike"] + 1); //display total disliked votes 
      break; 

     ##### respond votes for each content ######### 
     case 'fetch': 
      //get vote_up and vote_down value from db using unique_content_id 
      $qur   = mysqli_query($sql_con, "SELECT like, dislike FROM music_like WHERE music_id='$music_id' LIMIT 1"); 
      $row   = mysqli_fetch_assoc($qur); 

      //making sure value is not empty. 
      $like   = ($row["like"]) ? $row["like"] : 0; 
      $dislike  = ($row["dislike"]) ? $row["dislike"] : 0; 

      //build array for php json 
      $send_response = array(
       'like' => $like, 
       'dislike' => $dislike 
      ); 

      echo json_encode($send_response); //display json encoded values 
      break; 
    } 
} 
?> 

$(document).ready(function() { 

    //####### on page load, retrive votes for each content 
    $.each($('.voting_wrapper'), function(){ 

    //retrive music_id from this voting_wrapper element 
    var music_id = $(this).attr("id"); 

    //prepare post content 
    post_data = {'music_id':music_id, 'vote':'fetch'}; 

    //send our data to "vote_process.php" using jQuery $.post() 
    $.post('vote_process.php', post_data, function(response) { 

    //retrive votes from server, replace each vote count text 
    $('#'+music_id+' .like').text(response.like); 
    $('#'+music_id+' .dislike').text(response.dislike); 
    },'json'); 
    }); 

    //####### on button click, get user vote and send it to vote_process.php using jQuery $.post(). 

    $(".voting_wrapper .voting_btn").click(function (e) { 

     //get class name (down_button/up_button) of clicked element 
     var clicked_button = $(this).children().attr('class'); 

     //get unique ID from voted parent element 
     var music_id = $(this).parent().attr("id"); 

     if (clicked_button === 'down_button') //user disliked the content 
     { 
      //prepare post content 
      post_data = { 
       'music_id': music_id, 
       'vote': 'down' 
      }; 

      //send our data to "vote_process.php" using jQuery $.post() 
      $.post('vote_process.php', post_data, function (data) { 

       //replace vote down count text with new values 
       $('#' + music_id + ' .dislike').text(data); 

       //thank user for the dislike 
       alert("Thanks! Each Vote Counts, Even Dislikes!"); 

      }).fail(function (err) { 

       //alert user about the HTTP server error 
       alert(err.statusText); 
      }); 
     } 
     else if (clicked_button === 'up_button') //user liked the content 
     { 
      //prepare post content 
      post_data = { 
       'music_id': music_id, 
       'vote': 'up' 
      }; 

      //send our data to "vote_process.php" using jQuery $.post() 
      $.post('vote_process.php', post_data, function (data) { 

       //replace vote up count text with new values 
       $('#' + music_id + ' .like').text(data); 

       //thank user for liking the content 
       alert("Thanks! For Liking This Content."); 
      }).fail(function (err) { 

       //alert user about the HTTP server error 
       alert(err.statusText); 
      }); 
     } 

    }); 
    //end 
    }); 
+1

나는 온라인 인 덴터 도구로 들여 쓰기를 수정하기가 자유 롭다. 다음 번에 질문 할 때 코드를 읽을 수 있다면 사람들이 더 많은 도움을받을 수 있습니다. 하나의 주석, 당신의 INSERT 쿼리는 오타가 있습니다; 'value (...) '대신'values ​​(...)'이어야합니다. 그런데 45 행에 무엇이 있는지 알려줄 수 있습니까? – Christoffer

답변

3

LIKEreserved word 인 jQuery 코드 : 다음은 내 코드입니다. 따라서 예약어를 개체 이름 (열, 테이블 등)으로 사용할 때 항상 쿼리에서 백 마크를 사용하십시오.

예.

UPDATE music_like SET like=like+1 WHERE music_id='$music_id' 

UPDATE music_like SET `like`=`like`+1 WHERE music_id='$music_id' 

그리고

해야한다 :

SELECT like FROM music_like WHERE music_id='$music_id' LIMIT 1 

여기에 같은

SELECT `like` FROM music_like WHERE music_id='$music_id' LIMIT 1 
    ^^

에 당신은 변경해야
SELECT like, dislike FROM music_like WHERE music_id='$music_id' LIMIT 1 

그래서 당신의 쿼리가 유효하지 않게 할 실패

SELECT `like`, dislike FROM music_like WHERE music_id='$music_id' LIMIT 1 

합니다. 이로 인해 mysqli_query()이 실패하고 리소스 대신 부울 FALSE을 반환합니다. 그리고 그 이유는 당신이 게시 한 오류 메시지와 함께 mysqli_fetch_assoc() 실패합니다.