How can I limit each user to just one like per page? - php

I have a liking system, a bit like what facebook have. However, users can like pages as many times as possible.. How can I limit it to just one for each user like per page? The code I have is:
<?php
session_start() ;
$conn = mysqli_connect("","","","");
$p_id = $_GET['post_id'];
$result = mysqli_query($conn, "SELECT * FROM forum WHERE post_id = $p_id");
$row = mysqli_fetch_assoc($result);
mysqli_query($conn, "UPDATE forum SET likes=likes+1 WHERE post_id = '$p_id'") ;
?>
<?php
header("Location: forum.php?id=".$row['post_id']);
die();
?>

You have to track the user. Make a table with userid and postid

Related

Select the updated value using PHP

I'm working on attendance base on login and logout, Is there a way I can make this code work, I'm having a problem on after successfully update the data it will go to another notepad and in there it has a select query that will display the image and basic info of user. But when I tried to logout it displays different user info and images.
Here is my code for php:
<?php
include_once ('connection.php');
if(isset($_POST['submit']))
{
$username2 = $_POST['username2'];
$password1= $_POST['password1'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"SELECT tbl_visitor.Birthday,tbl_visitor.School_Company,tbl_visitor.Contact_number,tbl_visitor.Visitor_Address,tbl_visitor.image,tbl_visitor.Visitor_username,tbl_visitor.Visitor_id,tbl_visitor.Visitor_password,concat(Visitor_first_name,'',Visitor_last_name) as name,
tbl_visitor_form.Time_in,tbl_visitor_form.Time_out , tbl_visitor_form.Number FROM tbl_visitor LEFT JOIN tbl_visitor_form on tbl_visitor.Visitor_id = tbl_visitor_form.Visitor_id WHERE tbl_visitor.Visitor_username = '$username2' and tbl_visitor.Visitor_password = '$password1'
order by Number DESC limit 1");
$count = mysqli_num_rows($sql);
if ($count == 0) {
$_SESSION['error_message'] = "Incorrect username or password";
header("Location:logout.php?");
} else{
while ($row = mysqli_fetch_array($sql)) {
$username2 = $row['Visitor_username'];
$password1 = $row['Visitor_password'];
$name=$row['name'];
$id=$row['Visitor_id'];
$image=$row['image'];
if(empty($row['Time_in'])) {
header("location:visitorvalidate1.php");
} else if(empty($row['Time_out'])){
$InsertSql = "Update tbl_visitor_form set Time_out = '$time' where Visitor_username='$username2' and Visitor_password = '$password1' order by Number DESC limit 1 ";
$res = mysqli_query($conn, $InsertSql);
header("location:outsuccess.php?");
}else{
header("location:visitorvalidate1.php");
}
}
}
}
?>
here is my outsuccess.php:
<?php
include_once('connection.php');
$sql = "select Visitor_username, image, Visitor_name from tbl_visitor_form
order by Number DESC limit 1 ";
$result = mysqli_query($conn,$sql);
while( $row = mysqli_fetch_array($result)) {
$uname=$row['Visitor_username'];
$name=$row['Visitor_name'];
$image=$row['image'];
}
?>
the first query above is working but the second query in select has the problem, it selects different value after the update. for example, there is two user has already login user1 and user2 when I try to logout user1 it successfully updated but it displays the info of user2 instead of user1 on outsuccess.php. Hope you can help me fix this. Advance Thanks

How do I display a logged in user's images from the database?

Say if a user uploaded multiple images to their profile page, how would I display that specific user's images? I tried I did something like this:
$db = mysqli_connect("localhost", "root", "", "testdb");
$sql = "SELECT * FROM users ORDER BY id DESC";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<a href='profiles/uploads/".$row['image']."> ";
echo "<img id='img_div' title='".$row['image']."' alt='".$row['image']."' src='profiles/uploads/".$row['image']."'/>";
//echo "<p id='img_div'>".$row['desc']."</p>";
echo "</a>";
But I feel like this is terribly wrong because it is showing everyone's images and not the user's images. I tried looking up answers, but can't seem to find one.
You need a where clause. where id = 5, of course, replace the number with whatever user you are looking for.
Right now the query SELECT * FROM users ORDER BY id DESC is saying:
Select all columns from all users, ordering them by id.
Instead, you want something like:
SELECT * FROM users WHERE id = {user id} which states:
Select all columns from all users, where the id equals the user id.
As an aside, I'm not sure how you are setting up your database, but
if a user uploaded multiple images to their profile page, how would I display that specific user's images
makes me think that you should really be having separate tables, if you want to allow multiple pictures.
Try this:
You need to set user in sql query like id = 10.
And also missing ' last of this line: echo "<a href='profiles/uploads/".$row['image'].">";
<?php
$user_id = 10;
$db = mysqli_connect("localhost", "root", "", "testdb");
$sql = "SELECT * FROM users WHERE id = ". $user_id ." LIMIT 1";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<a href='profiles/uploads/". $row['image'] ."'>";
echo "<img id='img_div' title='".$row['image']."' alt='".$row['image']."' src='profiles/uploads/".$row['quotes']."'/>";
//echo "<p id='img_div'>".$row['desc']."</p>";
echo "</a>";
}
?>

PHP - Like limit for posts not working

So far, I am trying to limit the user from liking a certain post more than once. The problem is that when I click "like" on a certain post, the like count will go up every time I click the link, even though it should be limited to just one.
This is the user.php file:
echo "<a href='likes.php?id=$row[0]'>$row[6]</a>";
The "id=$row[0]" indicates to the id_post column in the database. "$row[6]" is the column in the database which shows the like count.
Here is the likes.php file:
<?php
include 'db.php';
connect();
$id = $_GET['id'];
$sql1 = "SELECT * FROM posts";
$result1 = mysqli_query($link, $sql1) or die(mysqli_error($link));
$row = mysqli_fetch_row($result);
if ($row[6] == 0) {
$sql2 = "UPDATE posts SET likes = likes + 1 WHERE id_post = '$id'";
$result2 = mysqli_query($link, $sql2) or die(mysqli_error($link));
}
if ($row[6] == 1) {
exit(header("Location: user.php"));
}
header("Location: user.php");
?>
What's the problem with my code?
SELECT * FROM posts
Your query is selecting all rows from the posts table, and not filtering based on the ID in your $id variable.
Try changing your query to:
SELECT * FROM posts WHERE COLUMN_NAME = '$id'
This way $row[6] will refer to the correct ID in your posts table.

Get data from different table and column

I am making a basic "blog" where different people with different user avatars are able to post content. I have 3 tables in MySQL, users, posts and following.
What I am trying to do is to display the avatar of the user who posted the post. And I can't get this to work, I have the code I've been working on below. It's little bit longer but it's mostly echo results in the end, I didn't include these in my example since the code got big. What is the correct way to get whoever posted what's avatar since it's in two different tables? I tried messing around with mysqli_multi_query earlier with no luck.
include "connect.php";
$user_id = $_SESSION["user_id"];
$query = "SELECT * FROM posts
WHERE user_id = $user_id OR (user_id IN (SELECT user2_id FROM following WHERE user1_id='$user_id'))
AND (SELECT user_img FROM users where user_img='$user_img')
ORDER BY timestamp DESC";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query);
while($row = mysqli_fetch_array($result)){
$postid = $row['id'];
$post = $row['post'];
$username = $row['username'];
$timestamp = $row['timestamp'];
$user_img = $row['user_img'];
$type = -1;
}

Displaying Profile image after login

I have the right idea. I am trying to display a profile image when the user logs in. I have a run time error. I have three different accounts set up. If I order the statment by User_Name, then I only get the ID of 2. If I order it by desc then I only get the ID of 3.. and so on and so forth. I am using two different naming conditions. mysql, and mysqli. I just need this to work as soon as possible. Here is my source code. Is their anyway to check for ID to equal the User_Name that I have within that mysql statment? Or is it more complex than I am thinking?
<?php
include "open_html.php";
include "htmlObody.php";
include "connect.php";
$query = mysqli_query($con, "SELECT User_Name FROM user");
$row = mysqli_fetch_assoc($query);
#setcookie("user", $row);
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("info");
$query2 = "SELECT ID FROM user ORDER BY User_Name";
$result = mysql_query($query2);
$row2 = mysql_fetch_array($result);
?>
<div>
<img src="user_file.php?ID=<?php echo $row2['ID']; ?>"/>
<br />
<?php echo $_COOKIE["user"]; ?>
</div>
<?php
include "htmlCbody.php";
include "close_html.php";
?>
My images were not being echoed by ID but by their names.
The query looks like this:
$sql = "SELECT ID, User_Image, User_Name FROM user WHERE User_Name = Image_Name";
Make sure that when the user creates a new user to have the user name equal the image name. So something like:
$userName = $_POST["user"];
$userImage = $_POST["user"];
$query = mysqli_query($con, "SELECT * FROM user INSERT INTO user_name, user_image VALUES ("userName", "userImage"));
That's not to insert an image to the database, but to make it so that you can correctly grab the profile image.
I used this link to upload images.

Categories