开发者

Trying to add a 'like' system

开发者 https://www.devze.com 2023-04-02 13:46 出处:网络
The following is passed to the browser: http://localhost/like.php?f=1 where f=1 is the article ID which I would like to like. There is a user system and I would only like them to be able to like the

The following is passed to the browser: http://localhost/like.php?f=1

where f=1 is the article ID which I would like to like. There is a user system and I would only like them to be able to like the article once (for obvious reasons). My开发者_如何学运维 code, pulls the information from the database about the article, then pulls through the user information (to get there ID from the current session) then checks to see if the user already liked this, and if not insert into the like database, or return back to the article page. Here is the code I have so far..

As you can see in the code, I've been trying to debug, but it's not going well...

<?php
include "connect.php";
session_start();
$feed = $_GET['f']; 
$feeddb = "SELECT * from feeddb where ID=$feed";
$feeddb2 = mysql_query($feeddb) or die("Whuuut?");
$feeddb3 = mysql_fetch_array($feeddb2);
if (isset($_SESSION['usrname']))
{
    $player=$_SESSION['usrname'];
    $userstats="SELECT * from feedus where tit='$player'";
    $userstats2=mysql_query($userstats) or die("Could not get user stats");
    $userstats3=mysql_fetch_array($userstats2);
    $feedli = "SELECT * from feedli where PID=$feed";
    $feedli2 = mysql_query($feedli) or die("Could not get likes");
    while($feedli3=mysql_fetch_array($feedli2))
    {
        if($userstats3['ID'] == $feedli3['UID'])
        {
            echo $userstats3[ID];
            echo $feedli3[UID];
        } else { 
            $updatelike =
                "INSERT into feedli (PID, UID) values('$feed','$userstats3[ID]')";
            $updatelike2 = mysql_query($updatelike);
            echo $userstats3[ID];
            echo $feedli3[UID];
        }
    }
}
else
{
}
?>

Any help would be appreciated!

Edit: For Clarity - I would like the code to insert the PID (article ID which is in $feed) and UID (which is the users ID in $userstats3[ID]) into the table FEEDLI if they haven't liked this article before, if they have, skip the code and go back to the previous article.


i think you are not using $feeddb3 in the code.. this might be usefull. also try to avoid sql injections.

 <?php
 session_start();
 include "connect.php";
 $feed = $_GET['f']; 

 if (isset($_SESSION['usrname']))
 {
    $player=$_SESSION['usrname'];
    $userstats2=mysql_query("SELECT * from feedus where tit='".mysql_real_escape_string(stripslashes($player))."'") or die("Could not get user stats");
    $userstats3=mysql_fetch_array($userstats2);

    $feedli2 = mysql_query("SELECT * from feedli where PID='".mysql_real_escape_string(stripslashes($feed))."' AND UID = '".$userstats3['ID']."'");

    if(!mysql_num_rows($feedli2))
    {
        $updatelike2 = mysql_query("INSERT into feedli (PID, UID) values('".mysql_real_escape_string(stripslashes($feed))."','".mysql_real_escape_string(stripslashes($userstats3['ID']))."')");
        echo $userstats3['ID'];
    }
    else
    {
        echo "already liked";   
    }

 }
 ?>


Before you execute the insert-query $updatelike = "INSERT into feedli (PID, UID) values('$feed','$userstats3[ID]')"; you should check wether there is a dataset with the transfered PID and UID or not. If there is one, do nothing... if not, execute the insert. I hope, I did not misunderstand your question.


First off, you may want to give your variables a bit more meaningfull names - just a tip for readability. Also, please watch for possible SQL-injection!

Secondly, as above, you can do this in a much faster way by checking if the tuple (user_id, feed_id) is present in your likes table. I've stopped writing as the above should work fine, though you could merge the feedli2 and userstats2 queries.

0

精彩评论

暂无评论...
验证码 换一张
取 消