Jump to content

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
https://forums.phpfreaks.com/topic/288164-help-required-in-incrementing-a-value/
Share on other sites

  • Replies 73
  • Created
  • Last Reply

Top Posters In This Topic

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.

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");
 
?>

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" );
?>

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.

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.

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" );
?>

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

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 by ginerjm

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());

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.