0

Hello Im writing a voting system for fun. The idea is that you vote on a music playlist, therefor the most popular song by vote gets played next. The track names as well as number of votes are stored in a mysql database. I am able to read data from my database and displaying the track names in a row with a button next to it. But I cant figure out how to make the button update the number of votes the track next to it has.

My database has a table called votecount with 3 columns called track_id track_name track_votes respectively. I havent included my update function below since it wont work at all and I suspect my fault lies with somewhere else.

I think of using the track id in a way of determining which track it should update by +1 each time the button next to it is pressed.

i have included connecting to the database in a separate file called connect.php

    <?php
    include 'connect.php';
    $sql = <<<SQL
        SELECT *
        FROM `votecount` 
    SQL;
    if(!$result = $conn->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    } 
    while($row = $result->fetch_assoc()){
        echo $row['track_name'] . " TotalVotes  ( " . $row['track_votes'] . " ) <form action='test.php' method'post'>
<input name='" . $row['track_name'] . "' type='submit' id='" . $row['track_id'] . "'value='vote'></input>
<br/></form>";   
}
?>

Any help will be greatly appreciated thanks in advance!

  • Add a hidden input that stores the `track_id` -> ``. Then on form post use the `track_id` to increase the vote. – Sean Apr 02 '15 at 14:41
  • It almost worked, however I only seam to be able to get the last ID of my list of tracks no matter which one I select? – Louis Scheepers Apr 03 '15 at 20:27

1 Answers1

1

There are few ways to do it, the easiest would be a link, to your script where you pass in the track id a query string:

echo "<a href='test.php?id=". $row['track_id'].">vote</a>";

Then inside of test.php retrieve the id $_GET['id'] then run the query to add a vote.

or you can use Ajax to perform a request to your script and pass in the track id.

meda
  • 45,103
  • 14
  • 92
  • 122