Updating a Specific Row in a mysql table - php

<?php
require_once 'config.php';
session_start();
$result = mysqli_query($link, "SELECT * FROM images where allowance=0 order by id desc");
while ($row = mysqli_fetch_array($result)) {
?>
<div id='img_div'>
<p><?php echo $row['image_text']; ?></p>
<img src="images/<?php echo $row['image']; ?>" >
<br><br>
<a role="button" class="btn btn-outline-success app" name="publish" href='admin.php?publish=true'>Publish</a>
<a role="button" class="btn btn-outline-danger app" name="reject" href='admin.php?reject=true'>Reject</a>
</div>
<?php
}
?>
<?php
if (isset($_GET['publish'])) {
$test=$_GET['id'];
$sql1 = "UPDATE images SET allowance='1' where id=$test";
if (mysqli_query($link, $sql1)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($link);
}
}
?>
What i am trying to do here is that i am admin.
when users upload a photo, it will have allowance value as '0'.
click here to see my mysql table with allowance value as 0
It wont be shown in timeline.
I will login through admin. So when i click publish, the allowance value is updated as 1 and thus it will be shown in timeline.
the problem here is that when i use this code all of the image gets updated as value 1.
click here to see my mysql table with allowance value as 0
I cannot find a specific WHERE condition for mysql command to update my allowance value..
or does anybody have any code for this concept..
i.e, user uploads photo it will be stored in database and then after admin approves it it will be shown in timeline...
Thanks in advance...

<?php
require_once 'config.php';
session_start();
if (isset($_GET['publish'])) {
$test=$_GET['id'];
$sql1 = "UPDATE images SET allowance='1' where id=$test";
if (mysqli_query($link, $sql1)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($link);
}
}
$result = mysqli_query($link, "SELECT * FROM images where allowance=0 order by id desc");
while ($row = mysqli_fetch_array($result)) { ?>
<div id='img_div'>
<p><?php echo $row['image_text']; ?></p>
<img src="images/<?php echo $row['image']; ?>" >
<br><br>
<a role="button" class="btn btn-outline-success app" name="publish" href='admin.php?publish=true&id=<?php echo $row['id']; ?>'>Publish</a>
<a role="button" class="btn btn-outline-danger app" name="reject" href='admin.php?reject=true'>Reject</a>
</div>
<?php } ?>

Related

SELECT query returns 1 row when there is more than 1

I'm working on a project for my university homework. Everything works right now except for my activity list. Issue is that whenever I try to view my activity list, it only returns 1 activity instead of all the activities that are still ongoing, when I manually run the query from phpMyAdmin, it returns all the queries but PHP doesn't seem to get it right and I have no idea why.
I have already tried running the query from phpMyAdmin and simplifying my query, also added a bunch of debugging code to see where I am going wrong but all the values are correct, the query is correct and phpMyAdmin runs the query just fine.
$query = "SELECT * FROM activities"; /*LIMIT " . $limit . ", " . $maxActivity;*/
$result = mysqli_query($link, $query);
echo $query;
if(!$result) {
echo "<p class='text-danger'>There are no activities to display.</p>";
} else {
while($row = mysqli_fetch_assoc($result)) {
$activity_id = $row['id'];
$activity_name = $row['name'];
$activity_description = $row['description'];
$activity_end_date = $row['endDate'];
$activity_start_date = $row['startDate'];
$activity_type = $row['type'];
$activityAuthor = $row['author'];
$activityPublished = $row['publishedAt'];
$activityLikes = $row['likes'];
$activityDislikes = $row['dislikes'];
$activityComments = $row['comments'];
$activityViews = $row['views'];
$query = "SELECT * FROM users WHERE Username = '$activityAuthor'";
$result = mysqli_query($link, $query);
if(!$result) {
die("MySQL Query Failed: " . mysqli_error($link));
}
$row = mysqli_fetch_assoc($result);
$authorID = $row['id'];
?>
<div class="row">
<div class="col-md-6">
<a href="#">
<img class="img-fluid rounded mb-3 mb-md-0" src="img/<?php echo "$activity_type";?>.jpeg" alt="">
</a>
</div>
<div class="col-md-6">
<h3><?php echo $activity_name;?></h3>
<p class="lead text-secondary">Proposed by <a class="text-secondary lead" href="profile.php?id=<?php echo $authorID;?>"><?php echo $activityAuthor;?></a> at <?php echo $activityPublished;?></p>
<p><?php echo $activity_description;?></p>
<p><strong>Starts at:</strong> <?php echo $activity_start_date;?></p>
<p><strong>Ends at:</strong> <?php echo $activity_end_date;?></p>
<a class="btn btn-outline-success" href="config/like.php?page=<?php echo $page;?>&post=<?php echo $activity_id;?>"><i class="far fa-thumbs-up"></i> <?php echo $activityLikes;?></a>
<a class="btn btn-outline-danger" href="config/dislike.php?page=<?php echo $page;?>&post=<?php echo $activity_id;?>"><i class="far fa-thumbs-down"></i> <?php echo $activityDislikes;?></a>
<button class="btn btn-outline-secondary"><i class="far fa-comment-dots"></i> <?php echo $activityComments;?></button>
<button class="btn btn-outline-secondary"><i class="far fa-eye"></i> <?php echo $activityViews;?></button>
<div class="mt-3"><a class="btn btn-primary" href="activity.php?post=<?php echo $activity_id;?>&page=<?php echo $page;?>&commentpage=1">View Activity</a></div>
</div>
</div>
<hr>
<?php }
}?>
<?php
$query = "SELECT * FROM activities";
$result = mysqli_query($link, $query);
if(!$result) {
echo "<p class='text-danger'>There are no activities to display.</p>";
} else {
while($row = mysqli_fetch_assoc($result)) {
$activity_id = $row['id'];
$activity_name = $row['name'];
$activity_description = $row['description'];
$activity_end_date = $row['endDate'];
$activity_start_date = $row['startDate'];
$activity_type = $row['type'];
$activityAuthor = $row['author'];
$activityPublished = $row['publishedAt'];
$activityLikes = $row['likes'];
$activityDislikes = $row['dislikes'];
$activityComments = $row['comments'];
$activityViews = $row['views'];
$query = "SELECT * FROM users WHERE Username = '$activityAuthor'";
$result = mysqli_query($link, $query);
if(!$result) {
die("MySQL Query Failed: " . mysqli_error($link));
}
$row = mysqli_fetch_assoc($result);
$authorID = $row['id'];
}
$output.="<div class='row'>
<div class='col-md-6'>
<a href='#'>
<img class='img-fluid rounded mb-3 mb-md-0' src='img/<?php echo '$activity_type';?>.jpeg' alt=''>
</a>
</div>
<div class='col-md-6'>
<h3><?php echo $activity_name;?></h3>
<p class='lead text-secondary'>Proposed by <a class='text-secondary lead' href='profile.php?id=<?php echo $authorID;?>'><?php echo $activityAuthor;?></a> at <?php echo $activityPublished;?></p>
<p><?php echo $activity_description;?></p>
<p><strong>Starts at:</strong> <?php echo $activity_start_date;?></p>
<p><strong>Ends at:</strong> <?php echo $activity_end_date;?></p>
<a class='btn btn-outline-success' href='config/like.php?page=<?php echo $page;?>&post=<?php echo $activity_id;?>'><i class='far fa-thumbs-up'></i> <?php echo $activityLikes;?></a>
<a class='btn btn-outline-danger' href='config/dislike.php?page=<?php echo $page;?>&post=<?php echo $activity_id;?>'><i class='far fa-thumbs-down'></i> <?php echo $activityDislikes;?></a>
<button class='btn btn-outline-secondary'><i class='far fa-comment-dots'></i> <?php echo $activityComments;?></button>
<button class='btn btn-outline-secondary'><i class='far fa-eye'></i> <?php echo $activityViews;?></button>
<div class='mt-3'><a class='btn btn-primary' href='activity.php?post=<?php echo $activity_id;?>&page=<?php echo $page;?>&commentpage=1'>View Activity</a></div>
</div>
</div>";
}
?>
<?php echo $output; ?>

Liking system, how do I fix it?

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; ?>

jquery output not working individually on php executed posts

i have a query that have many outputs and in those output i want to execute a jquery but that jquery is working in each & every output i want it to work only on the clicked one
this is my code
$query = "SELECT ph.likes, ph.image_url,ph.email,ph.username,ph.uid ,ph.id,ph.avatar_path
FROM photos as ph
inner join followers as fol
on fol.user_id = ph.uid
where fol.uid = '$id'
ORDER BY ph.image_url DESC ";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
while ($users = mysqli_fetch_assoc($fire)) {
$likes = $users['likes'];
$username = $users['username'];
$uid = $users['uid'];
$pixid = $users['id'];
$avatar_path5 = $users['avatar_path'];
?>
<div class="all" >
<div class="card" >
<div class="float" >
<div class="avatar" >
<img src="<?php echo $avatar_path5; ?>" width="100%" class="avatar">
</div>
<div class="username" style="font-weight: 600; size: 14px; text-decoration: none;">
<p><?php echo "<div><a href='users.php?id=".$users['uid']."'>
<h3>".$users['username']."</h3>
</div></a>"; ?></p>
</div>
</div>
<img src="<?php echo $users['image_url']?>" alt="Avatar" style="width:682px;">
<div class="container">
<h4><b><?php echo "<div><a href='users.php?id=".$users['uid']."'>
</div></a>";?></b></h4>
</div>
<span id="count" class="likes_count"><?php echo $users['likes']; ?> likes</span>
<div style="padding: 2px; margin-top: 5px;">
<?php
if (isset($_POST['liked'])) {
$postid = $_POST['postid'];
$result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
$row = mysqli_fetch_array($result)
or die(mysqli_error($con));
$n = $row['likes'];
mysqli_query($con, "INSERT INTO likes (user_id,username, post_id) VALUES ($id, '$fullname', $postid)")or die(mysqli_error($con));
mysqli_query($con, "UPDATE photos SET likes=$n+1 WHERE id=$postid")or die(mysqli_error($con));
echo $n+1;
exit();
}
if (isset($_POST['unliked'])) {
$postid = $_POST['postid'];
$result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
$row = mysqli_fetch_array($result)or die(mysqli_error($con));
$n = $row['likes'];
mysqli_query($con, "DELETE FROM likes WHERE post_id=$postid AND user_id=$id")or die(mysqli_error($con));
mysqli_query($con, "UPDATE photos SET likes=$n-1 WHERE id=$postid")or die(mysqli_error($con));
echo $n-1;
exit();
}
?>
</div>
<div>
<?php
// determine if user has already liked this post
$results = mysqli_query($con, "SELECT * FROM likes WHERE user_id=$id AND post_id=".$users['id']."")or die(mysqli_error($con));
if (mysqli_num_rows($results) == 1 ): ?>
<!-- user already likes post -->
<span class="unlike fas fa-heart animated bounceIn" data-id="<?php echo $users['id']; ?>"></span>
<span class="like hide far fa-heart" onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span>
<?php else: ?>
<!-- user has not yet liked post -->
<span class="like far fa-heart" onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span>
<span class="unlike hide fas fa-heart animated bounceIn" data-id="<?php echo $users['id']; ?>"></span>
<?php endif ?>
<a class="com" href="comments.php"> <span class="far fa-comment"></span></a>
<p>This is a paragraph.</p>
<button class="y<?php echo $users['id']?>">Toggle slideUp() and slideDown()</button>
<script>
$(document).ready(function(){
$(".y").click(function(){
$("p").slideToggle();
});
});
</script>
</div>
</div><br><br>
<?php } ?>
see this script in my code
<p>This is a paragraph.</p>
<button class="y<?php echo $users['id']?>">Toggle slideUp() and slideDown()</button>
<script>
$(document).ready(function(){
$(".y").click(function(){
$("p").slideToggle();
});
});
</script>
i want it to work only on the clicked post not on each & every post please help me out
for ex a if click on a post button so i want this script to work only for that
particular post's button not for every post button how to solve this
You seem to have the jquery selectors mixed up. From my reading of your PHP you apply a variable class name to the buttons that starts with y, then you select those with a class of y which won't match.
One way round this would be to also apply a consistent class to all the buttons (buttonclass in my example)
Then you're selecting all <p> elements to apply the slideToggle() instead of just the one whose button was clicked. You need to apply it only to the parent <p>.
This is one method of doing it:
$(document).ready(function(){
$(".buttonclass").click(function(){
$(this).parent().slideToggle();
});
});

How i can delete a database record over a link in PHP in the same page?

My Problem is that when i try to delete a database record over this link:
<?php
echo("<a href=\"./termine.php?action=edit&id=$id\">");
?>
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
<?php
echo("</a>");
?>
its will be reload the page and the page is white. Refer to the snapshot below.
The action code:
<?php
if (isset($_GET['action']) and $_GET['action']=="edit") {
$editId = $_GET['id'];
$edit = $db->prepare("UPDATE TABLE_NAME SET EDIT_FIELD = '100' WHERE ID = '" .$editId."' LIMIT 1");
}
?>

How to transfer the id selected on to another page for database in php?

I am new to PHP, i am now stuck at the transferring of id selected into another page.
this is my first page, home.php
<?php
require 'dbfunction.php';
$con = getDbConnect();
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM category");
while ($category = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<a class="thumbnail" href="product.php?cat=<?php echo $category['categoryid']; ?>">
<img src="<?php echo "img/" . $category['image'] ?>" width="200" height="200">
<div class="title"><h3><?php echo $category['title']; ?></h3></div>
</a>
</div>
<?php
}
mysqli_close($con);
}
?>
My second page, product.php
<?php
require 'dbfunction.php';
$con = getDbConnect();
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM product");
while ($product = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<a class="thumbnail" href="productInfo.php?cat=<?php echo $product['categoryid']; ?>&code=<?php echo $product['productname']; ?>">
<img src="<?php echo "img/" . $product['productimage']; ?>" width="250" height="220">
<div class="title"><h3><?php echo $product['productname']; ?></h3></div>
</a>
</div>
<?php
}
}
?>
data in my database,
category
categoryid PM2103
name PaperModel
image papermodel.jpg
categoryid GP4567
name GlossyPaper
image GlossyPaper.jpg
product
categoryid PM2103
productname PaperModel
Productimage papermodel.jpg
categoryid PM2103
productname PaperModel222
Productimage papermodel222.jpg
categoryid PM2103
productname PaperModel333
Productimage papermodel333.jpg
categoryid GP4567
productname GlossyPaper
Productimage GlossyPaper.jpg
categoryid GP4567
productname GlossyPaper222
Productimage GlossyPaper222.jpg
when a person select a category in home.php, it would bring the person into another page product.php, with all the product in the category list. I'm not sure what code i should use to link them together.
Your home.php page is alright ... modify product.php
<?php
require 'dbfunction.php';
$con = getDbConnect();
$cat=$_GET['cat'];
if (mysqli_connect_errno($con)) {
"Failed to connect to MySQL: " . mysqli_connect_error();
} else {
$result = mysqli_query($con, "SELECT * FROM product where categoryid='".$cat."'");
while ($product = mysqli_fetch_array($result)) {
?>
<div class="col-md-4">
<a class="thumbnail" href="productInfo.php?cat=<?php echo $product['categoryid']; ?>&code=<?php echo $product['productname']; ?>">
<img src="<?php echo "img/" . $product['productimage']; ?>" width="250" height="220">
<div class="title"><h3><?php echo $product['productname']; ?></h3></div>
</a>
</div>
<?php
}
}
?>
Change this in product.php
$cat_id = mysqli_escape_real_string($_GET['cat']);
$result = mysqli_query($con, "SELECT * FROM product WHERE categoryid={$cat_id}");
Note: But I'd recommend to use Prepared Statements/PDO to avoid SQL injection.
Read more on SQL injection: here, here

Categories