In my web application I have 2 buttons: one is "like", the other one is "dislike".
Users can't like their own profile.
I am looking for now a bit of code so that if a database check shows there is already a row for the user who wants to like or dislike that profile a message comes up: "You have already liked this user" or "You have already disliked this user"
<? if ($_POST['like']){
if(strtolower($view) == strtolower($fetchAccount['UserName'])){
echo "You cannot Like yourself!<br /><br/>";
} else {
mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
`LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
'{$fetchAccount['UserName']}', 'Liked', '$time')");
echo "You Liked {$fetchUser['UserName']}!<br /><br />";
}
}
if ($_POST['Dislike']){
if(strtolower($view) == strtolower($fetchAccount['UserName'])){
echo "You cannot DisLike yourself!<br /><br />";
} else {
mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
`LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
'{$fetchAccount['UserName']}', 'Disliked', '$time')");
echo "You DisLiked {$fetchUser['UserName']}!<br /><br />";
}
}
?>
Can anyone help please? Thanks in advance. :)
Here you go.
<?php
//-------Like section---------
if ($_POST['like']){
if(strtolower($view) == strtolower($fetchAccount['UserName'])){
echo "You cannot Like yourself!<br /><br/>";
} else {
//Check whether user 'liked' the other user already.
$selectQuery = "select * from `ProfileLikes` where `Profile` = '" .
$fetchUser['UserName'] ."' AND `Rated` = '" .
$fetchAccount['UserName'] . "' AND `LikedDisliked` = 'Liked' ";
$resultSetCheck = mysql_query($selectQuery);
$rowLikedExistsArray = mysql_fetch_assoc($resultSetCheck);
if (!empty($rowLikedExistsArray)) {
//He 'liked' it already!
echo "You have already liked this user! <br /><br/>";
} else {
//insert new 'like'
mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
`LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
'{$fetchAccount['UserName']}', 'Liked', '$time')");
echo "You Liked {$fetchUser['UserName']}!<br /><br />";
}
}
}
//-------Dislike section---------
if ($_POST['Dislike']){
if(strtolower($view) == strtolower($fetchAccount['UserName'])){
echo "You cannot DisLike yourself!<br /><br />";
} else {
//Check whether user 'disliked' the other user already.
$selectQuery = "select * from `ProfileLikes` where `Profile` = '" .
$fetchUser['UserName'] ."' AND `Rated` = '" .
$fetchAccount['UserName'] . "' AND `LikedDisliked` = 'Disliked' ";
$resultSetCheck = mysql_query($selectQuery);
$rowDislikedExistsArray = mysql_fetch_assoc($resultSetCheck);
if (!empty($rowDislikedExistsArray)) {
//He 'disliked' it already!
echo "You have already disliked this user! <br /><br/>";
} else {
//insert new dislike
mysql_query("INSERT INTO `ProfileLikes` (`id`, `Profile`, `Rated`,
`LikedDisliked`, `Date`) VALUES ('', '{$fetchUser['UserName']}',
'{$fetchAccount['UserName']}', 'Disliked', '$time')");
echo "You DisLiked {$fetchUser['UserName']}!<br /><br />";
}
}
}
?>
Note: I can see some optimizations to make. But dont wanna break your code flow.