lebaronfreddie Posted December 22, 2010 Share Posted December 22, 2010 Hi Guys! I'm new here, but I think this is where to post this... I'm trying to make a click counter for a button on a site. I have a database with different posts from users and I am displaying this button after the post. <form> <input type="button" value="Like" onClick="clickcount1.php" /> </form> I have different php files for each post and the number after clickcount corresponds with the ID of the entry in the database. What I'm trying to do is have the php code increment the total number number of clicks on a "Like" button in the "LikeTotal" column by 1. Here is the code I have so far... (mySQL connection data...) mysql_query($sql); $id = "ID"; $sql = "UPDATE comments SET LikeTotal = LikeTotal + 1 WHERE $id = 1"; mysql_close() Can anyone help me? If not can someone recommend a better way to create a like button that appears with data printed from a database for each individual record in that mySQL database? Thanks! Link to comment https://forums.phpfreaks.com/topic/222434-help-with-incrementing-code/ Share on other sites More sharing options...
requinix Posted December 23, 2010 Share Posted December 23, 2010 ... Before attempting that, you need a PHP and PHP+MySQL tutorial. Or two. Google is your friend. Link to comment https://forums.phpfreaks.com/topic/222434-help-with-incrementing-code/#findComment-1150549 Share on other sites More sharing options...
theverychap Posted December 30, 2010 Share Posted December 30, 2010 If this code is literally how you have it in your script: mysql_query($sql); $id = "ID"; $sql = "UPDATE comments SET LikeTotal = LikeTotal + 1 WHERE $id = 1"; mysql_close() Then it's never going to work! You'll want to declare the query before executing it: $sql = "UPDATE comments SET LikeTotal = LikeTotal + 1 WHERE ID = 1"; mysql_query($sql); Link to comment https://forums.phpfreaks.com/topic/222434-help-with-incrementing-code/#findComment-1152917 Share on other sites More sharing options...
Garethp Posted December 31, 2010 Share Posted December 31, 2010 Also, on top of that, it's not the right way to do it. After all, do you intend to create a new PHP file for every post? Here's what you want <a href="addClickCount.php?ID=1">Like</a> Remember to properly name your files for easy tracking later on, if your project gets too big //Create the Connection mysql_connect(HOST, USERNAME, PASSWORD); //Connect to DB mysql_select_db(DBNAME); //Get ID $ID = $_GET['ID']; //Sanatize it so that people can't use SQL Injection to hack into your database (Google it) $ID = mysql_real_escape_string($ID); //Execute SQL mysql_query("UPDATE `comments` SET LikeTotal = LikeTotal + 1 WHERE `ID`='$ID'"); mysql_close() But really, what you want to do is have a totally seperate table, called "Likes", with three columns. ID, Post, User. ID as the Primary Auto Increment Key, Post to define the ID of the post that is being Liked and User to define who is liking it. This way you can make sure that a person can only like a post once, and they can unlike it later, and you can keep track of what they like. If you're going into a big PHP Mysql Project, I suggest a little bit of research This is a good link to teach you the basics of propper MySQL usage (read over it even if you know everything, it might be useful) http://www.w3schools.com/php/php_mysql_intro.asp There's a series on Constructing your Database properly (also known as Normalization) which Art Langer describes really well. It's a bit long, but it's worth it Link to comment https://forums.phpfreaks.com/topic/222434-help-with-incrementing-code/#findComment-1153289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.