Lumana Posted December 14, 2022 Share Posted December 14, 2022 I have tried this code below to solve my problem , but in front end number is increasing buy in Mysql the numbers are not increasing <?php $con=mysqli_connect('localhost','root','','youtube'); $alias=$_GET['alias']; $sql="SELECT * FROM articles where alias='$alias'"; $res=mysqli_query($con,$sql); $row=mysqli_fetch_assoc($res); ?> <!DOCTYPE html> <html lang="en"> <head> <title>Like Dislike</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script> <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js" integrity="sha256-Lsk+CDPOzTapLoAzWW0G/WeQeViS3FMzywpzPZV8SXk=" crossorigin="anonymous"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script> </head> <body> <div class="container"> <div class="row main_box"> <div class="col-md-6 title"> <h3><?php echo $row['title']; ?></h3> </div> <div class="col-md-3"> <a href="javascript:void(0)" class="btn btn-info"> <span class="glyphicon glyphicon-thumbs-up" onclick="like_update('<?php echo $row['id']; ?>')">Like (<span id="like_loop_<?php echo $row['id']; ?>"><?php echo $row['like_count']; ?></span>)</span> </a> </div> <div class="col-md-3"> <a href="javascript:void(0)" class="btn btn-info"> <span class="glyphicon glyphicon-thumbs-down" onclick="dislike_update('<?php echo $row['id']; ?>')">Dislike (<span id="dislike_loop_<?php echo $row['id']; ?>"><?php echo $row['dislike_count']; ?></span>)</span> </a> </div> </div> <script> function like_update(id) { var cur_count=jQuery('#like_loop_'+id).html(); cur_count++; jQuery('#like_loop_'+id).html(cur_count); jQuery.ajax({ url:'update_count.php', type:'post', data:'type=like&id='+id, success:function(result){ } }); } function dislike_update(id) { var cur_count=jQuery('#dislike_loop_'+id).html(); cur_count++; jQuery('#dislike_loop_'+id).html(cur_count); jQuery.ajax({ url:'update_count.php', type:'post', data:'type=dislike&id='+id, success:function(result){ } }); } </script> </div> </body> </html> update_count.php <?php $con=mysqli_connect('localhost','root','','youtube'); $type=$_POST['type']; $id=$_POST['id']; if ($type=='like') { $sql="UPDATE articles SET like_count=like_count+1 WHERE id=$id"; } else { $sql="UPDATE articles SET dislike_count=dislike_count+1 WHERE id=$id"; } $res_count=mysqli_query($con,$sql); ?> Please help me my seniors here... Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/315646-like-dislike-front-end-working-but-in-database-number-is-not-increasing-pls-help-me/ Share on other sites More sharing options...
Solution Barand Posted December 14, 2022 Solution Share Posted December 14, 2022 Have you got php error reporting turned on? Have you looked at your browser developer tools ( network tab ) to check that the ajax info is being passed OK? Check that your queries are working correctly - put this code just before your mysqli_connect mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); Quote Link to comment https://forums.phpfreaks.com/topic/315646-like-dislike-front-end-working-but-in-database-number-is-not-increasing-pls-help-me/#findComment-1603539 Share on other sites More sharing options...
Lumana Posted December 16, 2022 Author Share Posted December 16, 2022 On 12/14/2022 at 11:16 PM, Barand said: Have you got php error reporting turned on? Have you looked at your browser developer tools ( network tab ) to check that the ajax info is being passed OK? Check that your queries are working correctly - put this code just before your mysqli_connect mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); Thanks for your reply sir, I have already checked the network Tab and its showing "update_count.php" can not found, though i placed the file in the same folder. But when i am doing like below its working : <?php $con=mysqli_connect('localhost','root','','youtube'); $sql="SELECT * FROM articles where alias='$it-is-fine'"; $res=mysqli_query($con,$sql); $row=mysqli_fetch_assoc($res); ?> But when I am doing like below, its not working : The URL not found they are showing is not the correct URL, Maybe because I am using htaccess, I am trying to solve the URL probelm, if you have any idea about that , pls share with me. Thanks sir <?php $con=mysqli_connect('localhost','root','','youtube'); $alias=$_GET['alias']; $sql="SELECT * FROM articles where alias='$alias'"; $res=mysqli_query($con,$sql); $row=mysqli_fetch_assoc($res); ?> Quote Link to comment https://forums.phpfreaks.com/topic/315646-like-dislike-front-end-working-but-in-database-number-is-not-increasing-pls-help-me/#findComment-1603581 Share on other sites More sharing options...
Barand Posted December 16, 2022 Share Posted December 16, 2022 You should check that $_GET['alias'] exists before attempting to use it. For example, this sets $alias to a default value of '0' if it does not exist. $alias = $_GET['alias'] ?? '0'' Quote Link to comment https://forums.phpfreaks.com/topic/315646-like-dislike-front-end-working-but-in-database-number-is-not-increasing-pls-help-me/#findComment-1603584 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.