Liking system, how do I fix it? - php

I have the following script for showing posts and liking them, but if I like one post it likes all the posts on the page, I can't think of another way to do it, can anyone give me some advice?
<?php
if ($sort == 1){
$result = $conn->query("SELECT * FROM posts ORDER BY date DESC LIMIT 4 ");
}
elseif($sort == 2)
{
$result = $conn->query("SELECT * FROM posts WHERE date > NOW() - INTERVAL 24 HOUR ORDER BY likes DESC");
}
elseif($sort == 3)
{
$result = $conn->query("SELECT * FROM posts ORDER BY likes DESC");
}
if ($result->num_rows > 0) :
while($row = mysqli_fetch_assoc($result)) : ?>
<div class="card mb-4">
<img class="card-img-top" src="<?php echo $row['image1'] ?>" alt="Card image cap">
<div class="card-body">
<h2 class="card-title"><?php print title; ?></h2>
<p class="card-text"><?php print text; ?></p>
Read More →
</div>
<div class="card-footer text-muted">
Posted on <?php print $row['date'] ?> by
<?php print $row['author']; ?>
<?php
$id=$row['id'];
if($_POST['like']) {
$update = "UPDATE posts set `likes` = `likes`+1 where `id` ='$id'";
if ($conn->query($update) === TRUE) {
} else {
echo "Error updating record: " . $conn->error;
}
} ?>
<form action="" method="POST">
<button type = "submit" value = "like" name='like'style="font-size:24px"><?php echo $row['likes']; ?><i class="fa fa-thumbs-o-up"></i>
</form>
</div>
</div>
<?php endwhile; endif; ?>

Your while loop contains the update query so your code should be change like this.
in order to get the id to like you just need to use a hidden field to post that id like in this code
<?php
if($_POST['like']) {
$id=$POST['id'];
$update = "UPDATE posts set `likes` = `likes`+1 where `id` ='$id'";
if ($conn->query($update) === TRUE) {
} else {
echo "Error updating record: " . $conn->error;
}
} ?>
<?php
if ($sort == 1){
$result = $conn->query("SELECT * FROM posts ORDER BY date DESC LIMIT 4 ");
}
elseif($sort == 2)
{
$result = $conn->query("SELECT * FROM posts WHERE date > NOW() - INTERVAL 24 HOUR ORDER BY likes DESC");
}
elseif($sort == 3)
{
$result = $conn->query("SELECT * FROM posts ORDER BY likes DESC");
}
if ($result->num_rows > 0) :
while($row = mysqli_fetch_assoc($result)) : ?>
<div class="card mb-4">
<img class="card-img-top" src="<?php echo $row['image1'] ?>" alt="Card image cap">
<div class="card-body">
<h2 class="card-title"><?php print title; ?></h2>
<p class="card-text"><?php print text; ?></p>
Read More →
</div>
<div class="card-footer text-muted">
Posted on <?php print $row['date'] ?> by
<?php print $row['author']; ?>
<form action="" method="POST">
<input name="id" type="hidden" value="<?php echo $row['id']; ?>">
<button type = "submit" value = "like" name='like'style="font-size:24px"><?php echo $row['likes']; ?><i class="fa fa-thumbs-o-up"></i>
</form>
</div>
</div>
<?php endwhile; endif; ?>

Related

How to dislpay all animals that are status = 1 in PHP

I'm making an adoption website for a project.
I'm trying to display all animals whose status = 1 but the animals whose status = 0 keeps appearing in the category.
Is something wrong with my code? suggestions and corrections are welcome.
Thank you!
function.php
function get_animals($cat_id='', $animal_id='')
{
global $con;
$query = "SELECT * FROM animals WHERE status= 1";
if($cat_id!='')
{
$query = "SELECT * FROM animals WHERE category_name='$cat_id'";
}
if ($animal_id!='')
{
$query = "SELECT * FROM animals WHERE id=$animal_id";
}
return $result = mysqli_query($con,$query);
}
This is my animals.php file
<?php
$cat_id = '';
if(isset($_GET['id']))
{
$cat_id = mysqli_real_escape_string($con,$_GET['id']);
}
$particular_animal = get_animals($cat_id);
?>
<!-- End Navigation -->
<!-- Product Grid -->
<div class="container mt-5 ">
<div class="row">
<?php
if(mysqli_num_rows($particular_animal))
{
while($row = mysqli_fetch_assoc($particular_animal))
{
?>
<div class="col-md-4 product-grid">
<div class="row">
<div class="image border border-info bg-light">
<a href="animal_details.php? a_id=<?php echo $row ['id'] ?>">
<img src="admin/image/<?php echo $row['img']?>" class="w-100" alt="">
</a>
<h4 class="text-center mt-2 text-info font-weight-bold"><?php echo $row ['name'] ?></h4>
<p class="text-center mt-2"><?php echo $row ['gender'] ?></p>
</div>
</div>
</div>
<?php
}
}
else
{
echo "record not here";
}
?>
</div>
</div>
You're overwriting your $query (the one with the status) when $cat_id or $animal_id is set. Instead, add to the WHERE clause of your first query:
$query = "SELECT * FROM animals WHERE status= 1";
if($cat_id != '') {
$query .= " AND category_name='$cat_id'";
}
if ($animal_id != '') {
$query .= " AND id=$animal_id";
}
If your function gets a category id or an animal id, then your query is overriden and your status criteria is lost. This is a 1-command solution:
function get_animals($cat_id='', $animal_id='')
{
return mysqli_query($con,"SELECT * FROM animals WHERE status= 1" .
($cat_id ? " AND category_name = {$cat_id} " : "").
($animal_id ? " AND animal_id = {$animal_id} " : ""));
}

How to fetch comments from mysql database without destroying my html tags

I want to give a little style for my comment section, here is the code without any css, but i want to give it some style
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<p>";
echo $row['author'];
echo "<br>";
echo $row['message'];
echo "<br>";
echo $row['time'];
echo "</p>";
}
} else {
echo "there are no comments!";
}
?>
</div>
<button>More comments</button>
and down here is my html section of which i want to appear while handling my php comments where USER, COMMENT and TIME are are stored in my database, here is the html, how can i echo the above variables into the below html tags ?
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5>USER</h5>
</div>
<div class="media-body response-text-right">
<p>COMMENT</p>
<ul>
<li>TIME</li>
<li>Reply</li>
</ul>
</div>
</div>
You can do like this:
<div id="comments">
<?php
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><?php echo $row['author']; ?></h5>
</div>
<div class="media-body response-text-right">
<p><?php echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li>Reply</li>
</ul>
</div>
</div>
<?php }
} else {
echo "there are no comments!";
}
?>
</div>
hope it will help you.

How do i insert these data from mysql into my webpage by php?

am trying to pull data from database of a number of associated rows into ROW1, ROW2 and ROW3 but using
<?php echo $row['']; ?>
but it's not working, please any idea
here is my code,
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
}
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $row['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $row['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
Well it looks like you are closing your while loop before you echo your results. Just move the closing bracket } after the closing </li>
Your code should look like this:
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $row['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $row['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
<?php
}
}
?>
Try this code
<?php
$sql = "SELECT * FROM songs ORDER BY id DESC LIMIT 2,1;";
$result = mysqli_query($con, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
foreach ($row as $data) {
?>
<li>
<a href="single.html"><img src="images/<?php echo $row['photo']; ?>" alt=""/>
</a>
<div class="slide-title"><h4><?php echo $data['song_name']; ?></div>
<div class="slide-title"><h4><?php echo $data['artist']; ?></div>
<div button class="btn btn-large btn-primary" type="button">BUY</div>
</li>
<?php
}
}
}
?>

fetching data from php Admin for display

I have a total 4 book of abc. I showing that author 1 book in book detail page. As well as i want to display remaining 3 books of author in below other books by this writer section. But in this section showing 1 book of that author. Please help me what can i do?
Book detail code:
<?php
if(isset($_GET['pid']) && !empty($_GET['pid'])){
$id = $_GET['pid'];
$result = $conn->query("SELECT * FROM bookrecord WHERE id ='$id' and status='a'");
if($result->num_rows > 0){
$imgData = $result->fetch_assoc();
extract($imgData);
?>
<div class="preview-pic tab-content">
<div class="tab-pane active" id="pic-1">
<img src="admin_pannel/book_images/<?php echo $imgData['file']; ?>" class="img-responsive" alt=""/>
</div>
</div>
<?php
}
}
?>
OTHER BOOKS BY THIS WRITER code:
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'");
if($result->num_rows > 0){
$row = $result->fetch_assoc();
extract($row);
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $row['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $row['book_title']; ?></h4>
<h5 class="text-center"><?php echo $row['author_name']; ?></h5>
</div>
<?php
}
}
?>
This is my (book-detail.php) page and i come from this link:
<a class="btnpoem" href="book-detail.php?pid=<?php echo htmlentities($row['id']);?>&auth_name=<?php echo htmlentities($row['author_name']);?>">Download</a>
You need to apply while() in your second code:-
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'");
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
extract($row);
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $row['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $row['book_title']; ?></h4>
<h5 class="text-center"><?php echo $row['author_name']; ?></h5>
</div>
<?php
}
}
}
?>
Note:- Your queries are Wide-Open for SQL-INJECTION.
So try to use prepared statements.
Reference:-
PHP: mysqli::prepare - Manual
PHP: PDO::prepare - Manual
Just change your code from this code and in this code you have to change your sql query a lil bit to get rows except first one because you have got first row(Image), and instead of while loop you can also use foreach
<?php
if(isset($_GET['auth_name']) && !empty($_GET['auth_name'])){
$auth_name = $_GET['auth_name'];
$result = $conn->query("SELECT * FROM bookrecord WHERE author_name ='$auth_name' and status='a'" and WHERE ID NOT IN (SELECT MIN(ID) FROM your_table GROUP BY USER_ID));
if($result->num_rows > 0){
foreach ($result as $key){
?>
<div class="col-md-2">
<img src="admin_pannel/book_images/<?php echo $key['file']; ?>" class="img-responsive center-block">
<h4 class="text-center"><?php echo $key['book_title']; ?></h4>
<h5 class="text-center"><?php echo $key['author_name']; ?></h5>
</div>
<?php
}
}
}
?>`

My user ranking system php sql help me

see screenshot development
as seen in the picture, where is the red circle should the number of user rankig appear. example: 1ts, 2th, 3rd...
I have tried everything but not me.
My table is users
ID Name username password Wins Loses
I leave my php code file profile.php
<?
require_once('values.inc.php');
require_once('func.inc.php');
if(!($_GET[user])) exit('Error.');
open_conn();
$res=mysql_query("SELECT ID, Name, username, Thumbnail, LinkText, LinkUrl, country FROM users WHERE Status='1' ORDER BY RAND() LIMIT 2");
while($row=mysql_fetch_assoc($res)) {
$username[] = $row[username];
$ID[] = $row[ID];
$Thumbnail[] = $row[Thumbnail];
$Name[] = $row[Name];
$LinkText[] = $row[LinkText];
$Model=mysql_query("SELECT ID, Name, username, Wins, Loses, Draws, Thumbnail, LinkText, LinkUrl, country, (Wins) / (Wins+Loses) AS WinPercent, (Wins+Loses+Draws) AS Matches, DATE_FORMAT(DateTime, '%d %M %Y') AS Date FROM users WHERE username='$_GET[user]' LIMIT 1");
$Model=mysql_fetch_assoc($Model);
$WinPercent=#round(($Model['Wins'] / ($Model['Wins']+$Model['Loses'])) * 100, 2);
$LosePercent=#round(($Model['Loses'] / ($Model['Wins']+$Model['Loses'])) * 100, 2);
if($Model['LinkText'] != '' && $Model['LinkUrl'] != '') {$Url="<br><br>$Model[LinkText]";}
if($row['LinkText'] != '' && $row['LinkUrl'] != '') {$Url[]="<br><br>$row[LinkText]<br><br>";}else{$Url[]='';}
}
close_conn();
?>
<?php if (isset($Model['username'])) { ?>
<div class="content-holder">
<div class="profile-holder">
<div class="profile-image">
<img src="images/uploads/<?php echo ucwords($Model['Thumbnail'])?>" class="photo">
</div>
<div id="photo-upload-holder">
<label for="pic" id="photo-upload-label">
<h1 class="txtShadow"><?php echo ucwords($Model['Name'])?></h1>
</label>
</div>
<!--<p class=\"userName\">#dada</p>-->
</div>
<div class="dashboard-Details overFlow">
<div class="leftColumn pull">
<div class="displayBlock"><br>
<i class="heart-icon"></i>
<span><?php echo ucwords($Model['Wins'])?></span>
</div><br>
<div class="displayBlock">
<i class="star-icon"></i>
<span><?php echo ($WinPercent)?>%</span>
</div>
</div>
<div class="rightColumn push">
<h2><?php echo ucwords($Model['username'])?></h2>
<?php if($Model['country']):?>
<h4> <?php echo $countrys; ?> <?php echo ucwords($Model['country'])?></h4>
<?php endif?>
<?php if($Model['LinkText']):?>
<h4> Instagram: #<?php echo ucwords($Model['LinkText'])?></h4>
<?php endif?>
</div>
<?php } else { ?>
<?php echo $profile14; ?>
use quotes here
$username[] = $row['username'];
$ID[] = $row['ID'];
$Thumbnail[] = $row['Thumbnail'];
$Name[] = $row['Name'];
$LinkText[] = $row['LinkText'];

Categories