display next record from database [PHP / MYSQL] - php

So I have a database, with a table in it. In the table, there's only one "ID" field
ID
1
2
3
..
what I want to do is to display the ID individually on my page, starting from ID=1. and when I click a button, the next ID will be displayed (ID=2, ID=3, .. each button click)
So this is my code
<?php
...
$id = (isset($_GET['id']) ? intval($_GET['id']) : 1);
$result = $db->query("SELECT * FROM id_table WHERE id>='$id' ORDER BY id ASC limit 2 ");
$row = $result->fetch_assoc();
...
<form action="" method="POST">
<h1 class="mb-3">
<?php echo($row['id'])?>
</h1>
<button onclick="" class="btn btn-primary btn-xl">Next</button>
</form>
ID is successfully displayed. But I've been struggling to make a working next button.
If this is a bad coding, please let me know and point me in right direction, so I can fix my mistakes. Thank you in advance, have a great day!

Related

Button to post value from fetched database data

I'm a beginner in PHP and PDO.
I'm trying to fetch database data from a database table, display it on a list and add a button that when clicked it adds the itemid value to another table with the name_id of the user.
The issue is even though the list is displaying the items in order and correctly,the button is not working 100%, when pressed on any item from the list it always posts the first itemid value from the database column.
Any help is appreciated.
echo ''.$itemid.'';
echo'<form action="" method="post"><button name="get" type="submit">CLICK ME</button></form>' ;
if(isset($_POST['get'])){
$query = "update table set fetched_item_id='".$row['item_id']."' where name_id='".$_SESSION['id']."' ";
if($query)
{
$query = $conn->prepare($query);
$query->bindParam('1', $itemid);
$query->execute();
echo "Success!";
exit;
}
I tried everything I could according to my knowledge.

request Mysql query that bring the name and description with the id number

I have content.php page that when I click on "More" it must give me the same content with the id number
<a class="btn text_3 color_3" href="contentdesc.php?contentid=<?=$content["id"]?>">more</a>
the problem is when i Click on More, it direct me to contentdesc.php and gives me the last row in the table by this query:
<?php
$select="SELECT * FROM content WHERE id HAVING COUNT(*) > 1";
$query=mysql_query($select);
while($row=mysql_fetch_assoc($query)) {
?>
<div class="grid_12">
<div class="box3">
<h3 class="text_4 equal" data-mh="3"><?=$row['name']?></h3>
<div class="divider"></div>
<p class="text_8 color_6"><?=$row['description']?></p>
</div>
</div>
<?php } ?>
Please can anyone help me to bring the content as same id number in the row?
Your SQL query in your contentdesc.php page is a little strange.
SELECT * FROM content WHERE id HAVING COUNT(*) > 1
What does it mean?
SELECT * return all columns
FROM content from this table
WHERE id where the value of the id column evaluates to true. That is, for every row with a nonzero id. (Probably not what you wanted.)
HAVING COUNT(*) > 1 if the table has two or more rows. (Probably not what you wanted).
It's probably incorrect. You probably need to use this SQL statement instead:
SELECT name, description FROM content WHERE id=DESIRED_ID_VALUE;
In php, it will look something like this.
$select="SELECT name, description FROM content WHERE id=" .
intval($_REQUEST["contentid"]) . ";";
The hyperlink in your first code line generates a URL that looks like this.
contentdesc.php?contentid=123
The value of $_REQUEST["contentid"] comes from that contentid parameter on the URL. intval() turns the value into an integer, to avoid sql injection.
Warning. The mysql_ calls you are using are obsolete and insecure. Please consider using mysqli_ or PDO instead.

PHP - I want the user to have no possibility to upvote more than one time

I am working on a small social network with PHP and MySQL
I want the users to have the possibility to upvote a post, and I already did this but the problem is that I want the user to be able to upvote only one time.
Here is my PHP code:
if (isset($_POST['heart']))
{
$identificateur = $_POST['hide'];
$Q = "UPDATE posts SET avis = avis + 1 where id=$identificateur ";
$bdd->query($Q);
}
This is the form of the upvote button :
<form action="p.php" method="POST">
<label><?php
if ($avis != 0)
{
echo $avis;
}
?></label>
<input type="hidden" value="<?php echo "$id" ?>" name="hide">
<input type="submit" value=" " id="heart" name="heart">
</form>
Every user should have the right to click on the up vote icon only once.
Thank you very much in advance.
You need to have a second table, that has two fields, post_id and voter_id. (post_voter).post_id has a foreign key containing the id of the post table and voter_id has foreign key to the user table. This table specifies voters of a post and posts that users have voted for. When a user tries to upvote a post, you must check post_voter table and get count of rows which have the post_id equal to the post's id and voter_id equal to the user's id. If count = 0, this means the user has not upvoted the post, yet, and he can vote for it now. Otherwise, the user upvoted the post already and he can't upvote it again.

Subtract number from column and add to another

What I want to do is have a button allows that when clicked takes away 1 from the column 'tickets' in the table 'event' and adds it to the column 'ticket' in the table 'user'.
Code for button
form action="sendellie.php" method="post">
<input type="submit" value="Buy tickets"/>
</form>
Snippet of Code for processing page (sendellie.php)
<?php
$sql = "UPDATE event SET tickets = tickets-1 WHERE name='Ellie Goulding'";
$sql = "UPDATE user SET ticket = ticket+1 WHERE name = '$uname'";
?>
The problem with the code above is that it does not work. It does not take away or add anything.
Any suggestions or tips would be much appreciated. Thanks

PHP/MySQL Like Button

I've made a 'like' button for my product pages with this code:
<?php
if('POST' == $_SERVER['REQUEST_METHOD']) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" value = "like"/>
</form>
Works like a charm excpet for one minor problem being that every visit to the page registers a 'like'.
Could someone help explain what i need to chnage/add in order that new 'likes' are only registered when the actual form is submitted?
Thanks
Dan
A better solution rather than submitting the page and the whole page reloading would be to make an AJAX request, this is how Facebook 'likes' work.
This can be achieved using the jQuery JavaScript library.
The general outline would be:-
1) Click button
2) Send AJAX request
3) Update HTML to show button has been clicked and prevent reclicking of button.
<?php
if($_POST['like']) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" value = "like" name='like'/>
</form>
This should work ;-)
<?php
if ($_POST['like']){
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" name="like" value = "like"/>
</form>
First of all - in your sql you have:
`product_id` = '1'
do not use id value as a string:
`product_id` = 1
About your problem:
Add another condition:
if ('POST' == $_SERVER['REQUEST_METHOD']) {
if ( !empty($_POST['submitType']) && ( $_POST['submitType'] == 'like' ) ) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
}
and in html:
<input type = "submit" name="submitType" value = "like"/>
Sounds like some kind of old question, but I wonder why noone has said, that op's approach doesn't sound quite right. You try to just count likes (set likes=likes+1). It has many disadvantages:
You miss information, who gave the like. Thus you won't be able to reconstruct the whole picture
Users won't be able to "undo" likes (as you don't record who liked the post)
In case of many concurrent likes I feel like you'd get some kind of data race or a long delays, because MySQL would need to process every request on a single field in order.
Much better idea is to create separate table in the DB named "product_likes" with columns like product_id, user_id, date. Of course, product id and user id should be unique together.
Thus you'll always know the full picture and will be able to see who liked the product. Even if accidentally you'll issue the second like from the same user about the same product, it won't be stored due to db constraints.
Also it will be possible to extend it to i.e. emotions-reactions, just by adding new column like "like_type" and updating the constraint correspondingly.

Categories