Like button not incrementing in sql database - php

I want to increment the number of likes on a post (say for postid 109) whenever I click Like.
I added a new column likecount and all values are 0 by default. They do not increment on clicking Like button.
I've tried a lot and searched for similar questions, but to no help.
HTML
<form method= POST >
<input type=submit value='' name=like class=likebutton>
</form>
PHP
if($_POST['like']) {
$sqlst = "UPDATE images SET `likecount` = `likecount`+1 WHERE `postid` = '109'";
$rslt= mysqli_query($db2, $sqlst);
}

I think query is failing because postid is integer and you are adding string value. Also use isset() for checking $_POST['like'], isset will execute script only when $_POST['like'] not empty Kindly try following
if(isset($_POST['like'])) {
$sqlst = "UPDATE images SET `likecount` = likecount + 1 WHERE `postid` = 109";
$rslt= mysqli_query($db2, $sqlst);
}

You can try
if(isset($_POST["like"]))
instead of
if($_POST['like'])

Related

DELETE FROM statement not working?

I've been trying to get my delete statement working.
This is how it should work: Whenever I press the delete button 'commentDelete' it should delete the comment, that has the commentID equal to the poster.
But instead, it only deletes the most previous comment, posted by the poster. I'm really confused, and can't figure out why.
Here's my code I tried:
function commentsDelete($conn) {
if(isset($_POST['commentsDelete'])){
$commentID = $_POST['commentID'];
$sql = "DELETE FROM comments WHERE commentID='$commentID'";
$result = mysqli_query($conn, $sql);
header("Location: commentpage.php");
}
}
The commentID is a integer
If the commentID column is numeric, then you should not be comparing against a quoted text string. Use this instead:
$sql = "DELETE FROM comments WHERE commentID=$commentID";
Remove the quotes from commentId if it is of numeric type in your database

MySQL table row will not UPDATE

I am trying to update a column in a row in a MySQL table. The column is the 'votes' column and when someone submits an HTML form there is a hidden input with a value of "1" that gets submit and posted. This is the code I am using to try to update the vote count:
if(isset($_POST['image_id']) && isset($_POST['vote'])){
$image_id = $mysqli->real_escape_string($_POST['image_id']);
$vote = $mysqli->real_escape_string($_POST['vote']);
$sql_users_vote = "SELECT * FROM users WHERE id='$image_id'";
$result_users_vote = $mysqli->query($sql_users_vote);
$row_vote = mysqli_fetch_array($result_users_vote);
$votes_count = $row_vote['votes'];
$new_votes = $votes_count + $vote;
$sql_vote = "UPDATE users WHERE id='$image_id' SET votes=$new_votes";
$result_vote = $mysqli->query($sql_vote);
}
I have echo'ed out the variable up until $sql_vote and $image_id, $vote, $votes_count and $new_votes all echo out the correct values. I'm guessing that there is a problem in the UPDATE syntax. I've checked it over and over but can't seem to find anything. I know that I don't have quotes around $new_votes in the UPDATE because I believe that is correct syntax. I've tried it with quotes and it doesn't work that way either.
Can someone help me identify the problem? Thanks!
Doesn't the SET come before the WHERE?
$sql_vote = "UPDATE users SET votes = $new_votes WHERE id = '$image_id'"
Or does it not matter?
$sql_vote = "UPDATE users SET votes=$new_votes WHERE id='$image_id'";

Adding and updating db row with PHP/AJAX

I've got a bit of a strange situation, Im building a site to keep track of outgoings, on my website you can insert an infinite number of 'outgoings' or 'bills' into my table, this is done using AJAX.
Once inserted, the record then shows up on the page and the user has an option to update, or delete the record.
My problem, is that when the record is inserted and posted back, I don't know what ID the record has fr4om the table so the update function doesn't work correctly. I've tried adding a a hidden field 'random-key' that when the user inserts a record, a random number is inserted and the this is posted back for my update function only I cant seem to get it working.
Has anybody an idea on how best to perform this?
Thanks
My code...
PHP form
<form id='bill-upd'>
<input type='hidden' value='".$info['rand']."' name='rand2' id='rand2'>
<input type='hidden' value='".$info['id']."' name='billid' id='billid'>
Total <input type='text' id='total' name='total' /><br />
Bill name<input type='text' id='bill-name' name='bill-name' /><br />
Bill descriptiion <input type='text' id='bill-description' name='bill-description' /><br />
Bill colour<input type='text' id='bill-colour' name='bill-colour' />
<input type='button' value='submit' onClick='updateBill();' />
</form>
UPDATE PHP PAGE
$uid = $_SESSION['oauth_id'];
$id = mysql_real_escape_string($_POST['billid']);
$bill = mysql_real_escape_string($_POST['total']);
$billname = mysql_real_escape_string($_POST['bill-name']);
$billdescription = mysql_real_escape_string($_POST['bill-description']);
$billcolour = mysql_real_escape_string($_POST['bill-colour']);
$rand = mysql_real_escape_string($_POST['rand2']);
#update Record
$query = mysql_query("UPDATE `outgoings` SET id = '$id', user_id = '$uid', bill = '$bill', bill_name = '$billname', bill_description = '$billdescription', bill_colour = '$billcolour', rand = '$rand' WHERE user_id = '$uid' AND rand = '$rand' ") or die(mysql_error());
While Inserting the new record, Get the newly added id (assuming that you have a unique id for each record) from the table and return that as the part of result you are returning after your ajax call. Then you can use that id for updating the record.
You can use the below script to send the details to be updated to the update page
$("#bill-upd").submit(function(e){
e.preventDefault();
$.post("update.php",{
billid: $("#billid").val()
total: $("#total").val();
bill-name : $("#bill-name").val();
bill-description : $("#bill-description").val();
bill-color: $("#bill-color").val();
},
function(result){
//Update / Show a message to user about the action.
//alert(result);
});
});
});
And in your update.php page, change your query like this
$query = mysql_query("UPDATE `outgoings` user_id = '$uid', bill = '$bill', bill_name = '$billname', bill_description = '$billdescription', bill_colour = '$billcolour', rand = '$rand' WHERE id = '$uid' AND rand = '$id' ") or die(mysql_error());
Assuming ID is the unique id / Primary key in the table which we can use to update a table record.
When you use an INSERT query, you can use mysql_insert_id() to get the value of the AUTO_INCREMENT field created for the inserted row. You can send this back with JSON so Javascript knows the ID of your newly inserted row. However consider if another user (using a different browser) adds a row while another user has the page open, Javascript won't get the new row. Example:
User 1 loads page and sees no rows.
User 1 adds Row #1.
User 2 loads page and sees Row #1.
User 2 adds Row #2 and now sees Rows #1,2
User 1 adds Row #3, but only sees Rows #1,3
And now User 2 will only see rows #1,2
You should probably really be SELECT * from table after every query to make sure you have all rows (including the ones other users may have inserted). You should also consider using PDO.

page count won´t add +1 to count field

I´ve been trying to make "most popular article" script
retrieve the most popular is no problem.... but I have tried every "add count+" methods
but the count field in my mysql always shows 0
This is my script
$add = "1";
$counter=mysql_query("SELECT * FROM news WHERE newsid = '".$newsid."'");
while ($ntcounter=mysql_fetch_array($counter)) {
mysql_query("UPDATE news SET count = '".$ntcounter[count]+$add."' WHERE newsid = '".$newsid."'")
}
I´m starting to think if the database is not updateable
Is there something I´m missing here?
You can do this in one go:
UPDATE news SET `count` = `count`+1 WHERE newsid = '".$newsid."'
EDIT:
<?php
//TURN ON ERROR REPORTING!!!
error_reporting(E_ALL);
//Type cast the variable to an integer, despite where its set
(int)$newsid=1;
//or
(int)$newsid=$_GET['id'];
//$newsid="1"; is setting 1 as a string
mysql_query("UPDATE news SET `count` = `count`+1 WHERE newsid=".$newsid);
?>
Note if your not checking or casting type. always remember to use mysql_real_escape_string()
You shouldn't be doing it like that at all. MySQL has built-in functionality for increasing values, you can just do:
UPDATE news SET count = count+1 WHERE newsid = '$newsid'
Yes. Your $add should be an integer.
$add = 1;

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