Thread: Any Help for me

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Any Help for me


    Hello, can some one please help me but I get errors like

    Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /Applications/MAMP/htdocs/live/vote_process.php on line 45

    This error shows up when I like(thumbs up) a content or article and the record in the database table never updates. But the error never shows up when I dislike(thumbs down) a content or article.

    The dislike(thumbs down) record will show on the page when you dislike but will show 0 once you refresh the page. is there better way to correct this? Below is my code:

    The PHP CODE

    <?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;

    }

    }
    ?>

    The JQUERY CODE
    $(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



    });
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,500
    Rep Power
    594
    1) Please enclose your code in [ PHP ] tags. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.
    2) DO NOT use the deprecated MySQL extensions, switch to PDO and prepared statements.
    3) You have absolutely no error checking in your code but the error means the query failed (as opposed to returning zero rows).
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    He's using mysqli, just not using prepared statements.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,500
    Rep Power
    594
    Yes, my apologies. However, the rest of my comments apply. In particular error checking needs to be added to see where the code is failing.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    I need help on this please


    Hello,

    I am new to this site but I need help. I have successfully displayed records from and database with php. The records displays nicely with a CSS class. But what I really want is to change the class name after every three record display. In other words, three records will be display with the same css class name like (column) but the fourth record will use a difference class name like(column-last). Can someone please help me with this?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,500
    Rep Power
    594
    Post that section of your code (presumably a loop) that outputs the records. Be sure to use [ PHP ] tags as previously indicated.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    I need help on this please


    Hi gw1500se, below is the code. I hope I did that right thing this time. The records displays nicely with a CSS class(col-4). But what I really want is to change the class name to (col-4 last) after every three record display. In other words, three records will be display with the same css class name(col-4) but the fourth record will use this (col-4 last) class name like(column-last).

    PHP Code:
    <?php foreach($result as $value) : ?>

        <div class="col-4">
            <a href="play.php?id=<?php echo $value['id']; ?>"><img alt="" src="<?php echo $value['image']; ?>" width="131px" height="120px"></a>
            <h3><a href="play.php?id=<?php echo $value['id']; ?>"><?php echo $value['title']; ?></a></h3>
            <p><strong><?php echo $value['username']; ?></strong> </p>
            <p><?php echo date('M j, g:i A'strtotime($value['date'])); ?></p>
        </div>

    <?php endforeach; ?>
  14. #8
  15. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Not

    PHP Code:
    if foreach($result as $value) : ?> 
    but

    PHP Code:
    <?php foreach($result as $value): ?>
    No if. "if foreach" doesn't make any sense.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!

IMN logo majestic logo threadwatch logo seochat tools logo