Jump to content

Help required in incrementing a value


mrfdes

Recommended Posts

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.

 

Link to comment
Share on other sites

  • Replies 73
  • Created
  • Last Reply

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")
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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");
 
?>
Link to comment
Share on other sites

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" );
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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" );
?>
Link to comment
Share on other sites

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 18

Notice: Use of undefined constant mysqli_error - assumed 'mysqli_error' in /home/jingleko/public_html/vlaamseradio.tk/top10/top10stem.php on line 18
mysqli_error

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 20

Warning: 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());
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.