mrfdes Posted May 1, 2014 Share Posted May 1, 2014 Forgive my ignorance, but I am quite new to PHP/MySQL. What I would like to achieve is, for a Top 10 on my radio station, I would like to allow the people to vote for individual songs, and when they do, increase the value of the number of votes in the database. I pass the value of the song they clicked on through the URL:http://www.vlaamseradio.tk/top10/top10stem.php?Song= It does display the song correctly on the page, saying "You voted for "Songname", but the votes value will not increase in the database. Here is the code I used: <?php $host="localhost"; $user="*****"; $pwd="******"; $dbname="jingleko_reloader"; mysqli_connect($host,$user,$pwd, $dbname); $song = mysql_real_escape_string($_GET['Song']); $songSafeHtml = htmlspecialchars($_GET['Song']); mysqli_query(" UPDATE voting SET Votes= Votes+ 1 WHERE Song = '$song' "); echo ("You voted for $songSafeHtml" ); ?> Any idea where my fault could be, please? All help will be very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/ Share on other sites More sharing options...
fastsol Posted May 1, 2014 Share Posted May 1, 2014 Looks like the query isn't running, don't you have to provide the $db connection link as the first parameter in the mysqli_query when using the procedural method. mysqli_query($link, "SELECT Name FROM City LIMIT 10") Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477844 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 Thank you very much for your reply. However, I have tried as you advised and it has not made any difference. The votes amount stays at 0. Thank you anyway. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477845 Share on other sites More sharing options...
fastsol Posted May 1, 2014 Share Posted May 1, 2014 Ok well then you need to start with basic diagnostics. Try this and see if it gives you an error why the query is failing. mysqli_query(" UPDATE voting SET Votes= Votes+ 1 WHERE Song = '$song' ") or die(mysqli_error($link)); // Change $link to whatever you are using for that. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477846 Share on other sites More sharing options...
Psycho Posted May 1, 2014 Share Posted May 1, 2014 Also, You are using the mysql_ version of real_escape_string Plus, it looks like you are putting the song title on the query string. Ideally, you should be using an ID and not a textual value for the song anyway. And, you are not checking for errors. Try this <?php //Get the passed value $song = trim($_GET['Song']); //Create DB connection $host="localhost"; $user="*****"; $pwd="******"; $dbname="jingleko_reloader"; $con = mysqli_connect($host,$user,$pwd, $dbname); //Update record count $songSQL = mysqli_real_escape_string($song); $query = "UPDATE voting SET Votes = Votes+ 1 WHERE Song = '$song'"; mysqli_query($query) or die(mysqli_error()); $songSafeHtml = htmlspecialchars($_GET['Song']); echo ("You voted for $songSafeHtml"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477847 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 OK, I did that, and now, I get a blank page where it is supposed to say "You voted for ThisorThatSong". Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477848 Share on other sites More sharing options...
Oliverkahn Posted May 1, 2014 Share Posted May 1, 2014 am sure this will work <?php $host="localhost"; $user="*****"; $pwd="******"; $dbname="jingleko_reloader"; mysqli_connect($host,$user,$pwd, $dbname); or die(mysqli_error()); mysqli_select_db( $dbname); or die(mysqli_error); $song = mysql_real_escape_string( $_GET['Song']); $songSafeHtml = htmlspecialchars($ _GET['Song']); if(isset($_GET['Song']) { $Votes=$Votes +1; } mysqli_query(" UPDATE voting SET Votes= $Votes WHERE Song = '$song' "); echo ("You voted for $ songSafeHtml" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477849 Share on other sites More sharing options...
fastsol Posted May 1, 2014 Share Posted May 1, 2014 I have never used the mysqli stuff but doesn't this line that Psycho provided now need to be mysqli_query($con, $query) or die(mysqli_error($con)); Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477850 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 Sorry, that first reply (just above) was for fastsol. And, to Psycho: I used your code, and once again, when I have clicked on one of the songs, I get a blank page when it is supposed to say "You voted for ...", and the value has not incremented. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477851 Share on other sites More sharing options...
ginerjm Posted May 1, 2014 Share Posted May 1, 2014 turn on php error checking as well. error_reporting(E_ALL | E_STRICT | E_NOTICE); ini_set('display_errors', '1'); Place these right after you session_start line or at the beginning of all of your php code Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477853 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 Thanks, OliverKhan, but now I am getting "mysqli_error" where "You voted for..." is supposed to appear. Thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477854 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 OK, after setting the error checking I now get: Warning: mysqli_connect(): (28000/1045): Access denied for user 'jingleko_reload'@'31.22.4.227' (using password: YES) in /home/jingleko/public_html/vlaamseradio.tk/top10/top10stem.php on line 17 Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/jingleko/public_html/vlaamseradio.tk/top10/top10stem.php on line 17 Line 16 and 17 are the following: 16 mysqli_connect($host,$user,$pwd, 17 $dbname) or die(mysqli_error());: Maybe this rings some bells. BTW, I am impressed with all the quick replies I am getting here. thanks so much, everyone. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477855 Share on other sites More sharing options...
Oliverkahn Posted May 1, 2014 Share Posted May 1, 2014 am not sure your database connection is working well, try this one.... and if its still not working that realy means your db connection has problem <?php $host="localhost"; $user="*****"; $pwd="******"; $dbname="jingleko_reloader"; mysqli_connect($host,$user,$pwd, $dbname); $song = mysql_real_escape_string( $_GET['Song']); $songSafeHtml = htmlspecialchars($ _GET['Song']); if(isset($_GET['Song']) { $Votes=$Votes +1; } mysqli_query(" UPDATE voting SET Votes= $Votes WHERE Song = '$song' "); echo ("You voted for $ songSafeHtml" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477857 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 I keep getting errors referring to line 18 now. The kines 16, 17 and 18 are this: mysqli_connect($host,$user,$pwd, $dbname) or die(mysqli_error()); mysqli_select_db($dbname) or die(mysqli_error()); The errors are: Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/jingleko/public_html/vlaamseradio.tk/top10/top10stem.php on line 18Notice: Use of undefined constant mysqli_error - assumed 'mysqli_error' in /home/jingleko/public_html/vlaamseradio.tk/top10/top10stem.php on line 18mysqli_error Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477858 Share on other sites More sharing options...
Oliverkahn Posted May 1, 2014 Share Posted May 1, 2014 that error means your update query is not proper Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477860 Share on other sites More sharing options...
Oliverkahn Posted May 1, 2014 Share Posted May 1, 2014 am sure you should chect ur db connection details Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477861 Share on other sites More sharing options...
ginerjm Posted May 1, 2014 Share Posted May 1, 2014 (edited) So many little things..... 1 - in your connect you had a semi in the middle. 2 - in your connect you are including the dbname, so you don't need to do a select db 3 - your check if 'song' was set had a semi in the middle again. 4 - the code incrementing votes is flawed; you haven't looked up the current value so why add now? 5 - you connected with mysqli but you escaped with MySQL corrected code (hopefully) session_start(); error_reporting(E_ALL | E_STRICT | E_NOTICE); ini_set('display_errors', '1'); .. .. .. $host="localhost"; $user="*****"; $pwd="******"; $dbname="jingleko_reloader"; $link = mysqli_connect($host,$user,$pwd,$dbname) or die(mysqli_error()); $song = mysqli_real_escape_string($link,$_GET['Song']); $songSafeHtml = htmlspecialchars($_GET['Song']); if (mysqli_query("UPDATE voting SET Votes= $Votes WHERE Song = '$song'")) echo "You voted for $songSafeHtml"; else die(mysqli_error()); Please try this verbatim. Edited May 1, 2014 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477862 Share on other sites More sharing options...
ginerjm Posted May 1, 2014 Share Posted May 1, 2014 I included a reference to $votes in the query by mistake. It should be "Votes = Votes + 1" still. (is 'votes' really capped in the table?) Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477863 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 Yes, it is capped. I now get this: Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/jingleko/public_html/vlaamseradio.tk/top10/top10stem.php on line 20Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/jingleko/public_html/vlaamseradio.tk/top10/top10stem.php on line 23 Lines 20 and 23 are: 20 if (mysqli_query("UPDATE voting SET Votes= Votes+1 WHERE Song = '$song'")) 23 die(mysqli_error()); Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477866 Share on other sites More sharing options...
ginerjm Posted May 1, 2014 Share Posted May 1, 2014 ok - so add "$link" as the only parm to mysqli_error() and as the first parm to the mysqli_query call. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477867 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 Ooh, I'm afraid you've lost me now. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477869 Share on other sites More sharing options...
mrfdes Posted May 1, 2014 Author Share Posted May 1, 2014 Like this? 20 if (mysqli_query($link "UPDATE voting SET Votes= Votes+1 WHERE Song = '$song'"))23 die $link (mysqli_error()); Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477872 Share on other sites More sharing options...
ginerjm Posted May 1, 2014 Share Posted May 1, 2014 Parms (parameters or arguments) go inside a function call's parentheses. And all arguments are separated by commas. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477873 Share on other sites More sharing options...
mrfdes Posted May 2, 2014 Author Share Posted May 2, 2014 YEEEEEEEEEEEEEEEEESSSSSSSSSSSSSSSSSSSS!!!!!!!! I only added the $link to the mysqli_query and now the message gets displayed and the vote incremented. Thank you SO MUCH everyone. Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477881 Share on other sites More sharing options...
ginerjm Posted May 2, 2014 Share Posted May 2, 2014 HTH! For future reference (and learning) - you need to study the manual (or some php language reference) and pick up on the syntax errors that were pretty pervasive in your code. The PHP manual is your best reference for looking up on how to use each function in PHP so you should make that link a shortcut cause you will find a need for it constantly. (I still do after almost 4 years of using php.) And most importantly - get rid of any reference to 'MySQL*' functions in your mind. Everything needs to be "mysqlI" or "pdo" now. Good luck in your future devl projects Quote Link to comment https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/#findComment-1477889 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.