abhishekdeveloper Posted February 17, 2012 Share Posted February 17, 2012 Dear All, I am developing a like/dislike voting system for a scientific experiment in which the user will cast vote on a number of images. I have already written the code for it but the code is unable to store the data in the database. Kindly help me out in this issue. Below is my code for it: HTML Form for Like/Dislike Button: <html> <head> <title> Like/Dislike buttons </title> </head> <body> <h1> Like/Dislike buttons </h1> <form name="form1" action="likedislike.php" method ="post"> <input type="submit" name="Like" value="Like"> <input type="submit" name="Dislike" value="dislike"> </form> </body> </html> PHP code for retrieving data and storing the vote in the database: <?php session_start(); ob_start(); $host="localhost"; $username="computat_abhi"; $password="1123581321"; $databasename="computat_button"; $tbl_name="record"; $db=mysql_connect ("localhost","computat_abhi","1123581321")or die("cannot connect"); mysql_select_db($databasename,$db); if( isset($_POST['Like']) ) { $sql = 'INSERT INTO record(Image Number,Like Counter) VALUES ("b1.php",1)'; $result = mysql_query($sql); } elseif(isset($_POST['Dislike'])){ $sql = 'INSERT INTO record(Image Number,Dislike Counter) VALUES ("b1.php",1)'; $result = mysql_query($sql); } ob_end_flush(); ?> Thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/257169-error-while-storing-the-votes-in-the-database/ Share on other sites More sharing options...
The Little Guy Posted February 17, 2012 Share Posted February 17, 2012 if you column names have spaces in it either: A. Replace the spaces with an underscore or remove them (recommended option). B. place ` tic marks around the column names. Quote Link to comment https://forums.phpfreaks.com/topic/257169-error-while-storing-the-votes-in-the-database/#findComment-1318271 Share on other sites More sharing options...
MMDE Posted February 17, 2012 Share Posted February 17, 2012 Try this, and tell us what appears on the screen: <?php session_start(); ob_start(); $host="localhost"; $username="computat_abhi"; $password="1123581321"; $databasename="computat_button"; $tbl_name="record"; $db=mysql_connect ("localhost", "computat_abhi", "1123581321")or die(mysql_error()); mysql_select_db($databasename, $db) or die(mysql_error()); if( isset($_POST['Like']) ) { $sql = 'INSERT INTO record(Image Number, Like Counter) VALUES (\'b1.php\', 1)'; mysql_query($sql, $db) or die(mysql_error()); echo 'Like vote was registered'; } elseif(isset($_POST['Dislike'])){ $sql = 'INSERT INTO record(Image Number, Dislike Counter) VALUES (\'b1.php\', 1)'; mysql_query($sql, $db) or die(mysql_error()); echo 'Dislike vote was registered'; } ob_end_flush(); ?> And you should totally go for the system I told you about before, where you register each vote with a vote id, image id, user id, vote value (boolean), much better to keep track of who has voted what, and make sure nobody vote more than once on the same image, and remove false votes from banned users. What your script does now is to add another row in the table which says b1.php in the image column and 1 in either like or dislike column. Unless you have set like or dislike column to have a default value, the queries won't work and will give you an error. Since this is your system, that either 1 is placed in like or dislike column, then I would much rather recommend you to have one row called "like" that stores boolean values, 1 for like 0 for dislike. You can then count the number of rows with that image name that has either 0 or 1 likes, and by that you know how many likes it and how many doesn't. If what you want to do is to have one column for each image and make the number in the like and dislike column increase, then that's not what your script does at all. You would need to look into the UPDATE query for that. 'UPDATE record SET Like Counter = Like Counter+1 WHERE Image Number = \'b1.php\'' 'UPDATE record SET Dislike Counter = Dislike Counter+1 WHERE Image Number = \'b1.php\'' http://www.w3schools.com/php/php_mysql_update.asp But to be able to update a row with that Image Number, it must exist, so you will have to check if the row exist first, and if it doesn't you must insert a row. One way to check if it exists could be: $query = 'SELECT * FROM record WHERE Image Number = \'b1.php\''; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)==1){ // 1 row with that Image Number exists } You will soon realize that the system I proposed for you earlier is pretty much just the same amount of work, just a lot better. Quote Link to comment https://forums.phpfreaks.com/topic/257169-error-while-storing-the-votes-in-the-database/#findComment-1318320 Share on other sites More sharing options...
abhishekdeveloper Posted February 17, 2012 Author Share Posted February 17, 2012 @MMDE - please check your inbox, i have sent you a message regarding the problem. Thanks for your reply. Try this, and tell us what appears on the screen: <?php session_start(); ob_start(); $host="localhost"; $username="computat_abhi"; $password="1123581321"; $databasename="computat_button"; $tbl_name="record"; $db=mysql_connect ("localhost", "computat_abhi", "1123581321")or die(mysql_error()); mysql_select_db($databasename, $db) or die(mysql_error()); if( isset($_POST['Like']) ) { $sql = 'INSERT INTO record(Image Number, Like Counter) VALUES (\'b1.php\', 1)'; mysql_query($sql, $db) or die(mysql_error()); echo 'Like vote was registered'; } elseif(isset($_POST['Dislike'])){ $sql = 'INSERT INTO record(Image Number, Dislike Counter) VALUES (\'b1.php\', 1)'; mysql_query($sql, $db) or die(mysql_error()); echo 'Dislike vote was registered'; } ob_end_flush(); ?> And you should totally go for the system I told you about before, where you register each vote with a vote id, image id, user id, vote value (boolean), much better to keep track of who has voted what, and make sure nobody vote more than once on the same image, and remove false votes from banned users. What your script does now is to add another row in the table which says b1.php in the image column and 1 in either like or dislike column. Unless you have set like or dislike column to have a default value, the queries won't work and will give you an error. Since this is your system, that either 1 is placed in like or dislike column, then I would much rather recommend you to have one row called "like" that stores boolean values, 1 for like 0 for dislike. You can then count the number of rows with that image name that has either 0 or 1 likes, and by that you know how many likes it and how many doesn't. If what you want to do is to have one column for each image and make the number in the like and dislike column increase, then that's not what your script does at all. You would need to look into the UPDATE query for that. 'UPDATE record SET Like Counter = Like Counter+1 WHERE Image Number = \'b1.php\'' 'UPDATE record SET Dislike Counter = Dislike Counter+1 WHERE Image Number = \'b1.php\'' http://www.w3schools.com/php/php_mysql_update.asp But to be able to update a row with that Image Number, it must exist, so you will have to check if the row exist first, and if it doesn't you must insert a row. One way to check if it exists could be: $query = 'SELECT * FROM record WHERE Image Number = \'b1.php\''; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)==1){ // 1 row with that Image Number exists } You will soon realize that the system I proposed for you earlier is pretty much just the same amount of work, just a lot better. Quote Link to comment https://forums.phpfreaks.com/topic/257169-error-while-storing-the-votes-in-the-database/#findComment-1318443 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.