PHP - Like limit for posts not working - php

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.

Related

Tryning to get the right information from mysql

I'm trying to call the correct mysql information.
In my first query, I check if the order is available and take the post_id so I can make a SELECT that includes all post_id's for my while.
In the first query is there many post_id's so I need to connecte tto many id's
An example that does not work but explain what I mean:
$query103 = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_posts`
where ID='order_id1' and ID='order_id2' and ID='order_id3'
") or die(mysqli_error($conn));
The $orderidtilsoog can be more IDs and not just 1
Hope you understand me
The is the completaly code:
$query = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_postmeta` where meta_value='$emailconvert' ORDER BY meta_id ") or die(mysqli_error($conn));
echo "Kunden er fundet og ligger på https://v1.com";
while($row55 = mysqli_fetch_array($query)) {
$orderidtilsoog = $row55['post_id']. ",";
echo $orderidtilsoog;
if(mysqli_num_rows($query) == '0') {
}else{
$query103 = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_posts` where ID
IN ('$orderidtilsoog') ") or die(mysqli_error($conn));
}
Just use SELECT * FROM $v1.wpd2_posts where ID IN('$orderidtilsoog')

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

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

Displaying a SQL table data for a specific user

I want to display the table participantes with the columns sorteo, nombre and fecha.
The user info is on another table sellify_users (usern column).
I want to display only that user data using:
SELECT * FROM participantes WHERE nombre = 'usern'
But usern is not in the same table, so if possible I want to call the sellify_users to get the usern data.
<?php
$user = 'database_user';
$password = 'database_pass';
$database="database_name";
mysql_connect(localhost,$user, $password);
#mysql_select_db($database) or die( "Unable to select database");
echo $query = "SELECT * FROM participantes WHERE nombre='usern'";
$result = mysql_query($query);
mysql_close();
?>
If you meant that a user has a record in the table sellify_users and you need to find the usern from it in order to use it in the next query:
$query = "SELECT * FROM participantes WHERE nombre='usern'";
Then all you need to do is to run a query for the table sellify_users first and get the value usern from it, store it in a variable and then use that in your second query. Something like:
$query1 = "SELECT * FROM sellify_users";
$result1 = mysqli_query($con, $query1);
$row1 = mysqli_fetch_assoc($result1);
$usern = $row1['usern'];
$query2 = "SELECT * FROM participantes WHERE nombre='usern'";
$result2 = mysqli_query($con, $query2);
while($row2 = mysqli_fetch_assoc($result2)){
echo $row2['ColumnNameHere1'];
echo $row2['ColumnNameHere2'];
}
Notice: I've passed $con which denotes a connection variable, i.e.
you should read about mysqli or PDO and if there's anything you do not understand, feel free to shoot a query.
EDIT:
If by any chance you are trying to use the last inserted record, you should look for mysqli_insert_id($con); and use that instead.

How to do two DB queries at once in PHP?

I have two queries but can I have them as just one
$sql = ("SELECT * FROM table WHERE title='$title' LIMIT 1");
$sql = ("UPDATE table SET views = views+1 where title='$title' ");
$query = mysql_query($sql) or die("Error Connecting To db");
At the moment the 2nd one works but not the first one.
This is your code:
$sql = ("SELECT * FROM table WHERE title='$title' LIMIT 1");
$sql = ("UPDATE table SET views = views+1 where title='$title' ");
$query = mysql_query($sql) or die("Error Connecting To db");
First, the first $sql will not run because you are overwriting the contents of that first $sql with new content in the next line.
Then just looking at your logic, here is your first query:
SELECT * FROM table WHERE title='$title' LIMIT 1
I assume that is to get the entry with the title equalling $title and nothing else, right? But then your next query is this:
UPDATE table SET views = views+1 where title='$title'
You are updating the value where title='$title' anyway. So the first query is not even needed. So problem solved, right? Well you might want to add the LIMIT 1 to the second query like this:
UPDATE table SET views = views+1 where title='$title' LIMIT 1
But honestly the logic of updating a DB based on whether an item title matches seems messy. What if two items have the same title? Yes, you are limiting things by 1 but by what criteria? If you have three items with the same title, which gets updated?
You need more differentiation for your app structure. But I think that is fair for what is essentially a newbie question.
Run the two queries separately
$sql1 = ("SELECT * FROM table WHERE title='$title' LIMIT 1");
$qry = mysql_query($sql1);
if (mysql_num_rows($qry) > 0)
{
while ($res = mysql_fetch_object($qry))
{
// -- contents --
}
$sql = ("UPDATE table SET views = views+1 where title='$title' ");
$query = mysql_query($sql) or die("Error Connecting To db");
}

PHP - MySql Join -

I was trying to get some details from MySql database, but i was working so long and tried so many ways that i am completely lost now :s
What i need to GET is two details which depend on information from 3 tables. I need to get $title and $siteurl (from table_sites) where current user did not click it before, AND siteurl owner still have remaining credits...
Here is my database:
USER:
id
username
password
credits
active
clicks
SITES:
id
userid
title
siteurl
clicks
active
weight
CLICKS:
id
siteid
byuserid
i tried with this MySql query:
include('db_con.php');
mysql_connect("$dbhost", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$qrym = mysql_query("SELECT * FROM `users` WHERE username='$username' LIMIT 1") or die (mysql_error());
while($mem = mysql_fetch_object($qrym)){
$uid = $row->id;
}
$qrys = mysql_query("SELECT * FROM sites, clicks WHERE clicks.byuserid != '$uid' and sites.userid != '$uid' and sites.active = '1' ORDER BY sites.weight DESC, sites.id DESC LIMIT 1") or die (mysql_error());
if(mysql_num_rows($qrys)!=0){
while($row = mysql_fetch_object($qrys)){
$title = $row->title;
$siteurl = $row->siteurl;
echo "$title $siteurl";
}
} else {
echo "No more sites";
}
No errors, but whatever i try result is No more sites! How to JOIN this query correctly?
Maybe do
while($row = mysql_fetch_object($qrym)){
$uid = $row->id;
instead of
while($mem = mysql_fetch_object($qrym)){
$uid = $row->id;
You probably want a query like this:
SELECT [the columns you need]
FROM sites
LEFT JOIN clicks ON clicks.siteid = sites.id
AND clicks.byuserid = [current user id]
WHERE sites.active = 1 AND clicks.id IS NULL
ORDER BY sites.weight DESC, sites.id DESC
LIMIT 1
As gpojd noted above, you must must MUST sanitize your inputs before using them in a query. Fix your first query's code:
$qrym = mysql_query("SELECT * FROM `users`
WHERE username='" . mysql_real_escape_string($username) . "' LIMIT 1");
When fetching only a single row, as your first query does, there is absolutely NO need for a while() loop to retrieve the data.
That, and observe the comments in the code block:
$qrym = mysql_query("SELECT * FROM `users` WHERE username='$username' LIMIT 1") or die (mysql_error());
while($mem = mysql_fetch_object($qrym)){
^^^--- you fetch into $mem
$uid = $row->id;
^^^--- but try to retrieve from $row?
}
Try this instead:
$sql = "SELECT ...";
$qrym = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($qrym);
$uid = $row['uid'];

Categories