Hi everybody!
I'm trying to create 2 image buttons (like,dislike). When they are submitted, it should be run a query for like/dislike a definite song. My code is:
<?php

    $table = mysqli_query($mysqli,"SELECT  song_id, song_name, janr_name,  singers.names FROM `songs` INNER JOIN `singers` ON songs.singer_id = singers.singer_id


INNER JOIN `janr_songs` WHERE janr_songs.janr_id = songs.janr_id ");
$row = mysqli_fetch_array($table); 
{   
    echo "<table border='1' >";  
    echo "<tr><th>№</th><th>Song</th><th>Artist</th><th>Song genre</th><th>Likes</th><th>Dislikes</th><th>LIKED</th><th>DISLIKED</th></tr>";  

while($row = mysqli_fetch_array($table))
{
    echo "<tr><td>".$row['song_id']."</td><td>".$row['song_name']."</td><td>".$row['names']."</td><td>".$row['janr_name']."</td><td>"
    .$row['likes']."</td><td>".$row['dislikes']."</td>;"; echo "<th><input type='image'  name='submit' value='like'  src='like.jpg'></th>;
    "; echo "<th><input type='image'  name='submit' value='dislike' id='".$row['song_id']. "' src='2.jpg'></th>;</tr>"; 
} "<br>";
echo "</table>";"<br>"; 
}
This code doesn't work. Image buttons are set and they appear, but there is no functionality. If the buttons are not in the table, they work. Where is the mistake? :)

Recommended Answers

All 18 Replies

Well, there seems to be missing an INSERT or UPDATE query to insert/update the like/dislike, and there seems to be missing a <form> that the buttons use.

So where/when exactly is stuff going wrong?

    if (isset($_POST['submit'])) {
if ($_POST['submit'] == 'like') {
    $insert = mysqli_query($mysqli, "INSERT INTO `voting` (`user_id`,`song_id`,`likes`) VALUES ('".$_SESSION['user_info']['user_id']."','2', 1)");
}

That's code for submitting. Query doesn't work when button is in the table row.

And that's the form:

<html>
<body>
<h3> Not voted songs </h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="button" name="button" onclick="findresult()" value="Sort by Artist">
    <input type="button" name="butt1" value="Sort by Genre">
    <input type="image"  name="submit" value="like" src='like.jpg'>  
    <input type="image"  name="submit" value="dislike"  src='2.jpg'> 
</form>
</body>
</html> 

Well, any <input> of which you want to use the value must (generally spoken) be inside <form> element. When you press the submit button, all input inside the <form> element that submit button is in is gathered and sent to the url that you specify under the form's action attribute. So, if you have buttons or inputs outside a form element, they won't work (except if you use Javascript to create a "workaround").

So, how should I make it. It sholud be a table, in which there are song names, artists and son on and on each row users could vote for each song - buttons like/dislike. In which way to create that?

You could include a form in each of your table rows. That form should then include the like/dislike buttons.

Or you can make those buttons pass on GET variables to your page by linking to (for example) "your_page.php?action=like". Your form passes (in this case) POST data to your page. A simple link would pass GET data. You wouldn't need a form, but you need to obtain your data using $_GET instead of $_POST.

If I use form, what I should change in this code. In each row I've put input button.

<?php 
    $table = mysqli_query($mysqli,"SELECT  song_id, song_name, janr_name,  singers.names FROM `songs` INNER JOIN `singers` ON songs.singer_id = singers.singer_id
    INNER JOIN `janr_songs` WHERE janr_songs.janr_id = songs.janr_id ");
    $row = mysqli_fetch_array($table); 
    {   
        echo "<table border='1' >";  
        echo "<tr><th>№</th><th>Song</th><th>Artist</th><th>Song genre</th><th>Likes</th><th>Dislikes</th><th>LIKED</th><th>DISLIKED</th></tr>"; 



while($row = mysqli_fetch_array($table))
{
    echo "<tr><td>".$row['song_id']."</td><td>".$row['song_name']."</td><td>".$row['names']."</td><td>".$row['janr_name']."</td><td>"
    .$row['likes']."</td><td>".$row['dislikes']."</td>;"; echo "<th><input type='image'  name='submit' value='like'  src='like.jpg'></th>;
    "; echo "<th><input type='image'  name='submit' value='dislike' id='".$row['song_id']. "' src='2.jpg'></th>;</tr>"; 
} "<br>";
echo "</table>";"<br>"; 
}

As I said, you should either include your buttons in a <form>, or you should use regular links that send GET data.

<?php
while($row = mysqli_fetch_array($table))
{
    ?>
    <tr>
        <td>
            <?php echo $row['song_id']; ?>
        </td>
        <td>
            <?php echo $row['song_name']; ?>
        </td>
        <td>
            <?php echo $row['janr_name']; ?>
        </td>
        <td>
            <?php echo $row['likes']; ?>
        </td>
        <td>
            <?php echo $row['dislike']; ?>
        </td>
        <td>
            <form action="" method="post">
                <input type="hidden" name="song_id" value="<?php echo $row['song_id']; ?>"><!-- This might be useful if you want to identify which song receives a submission. -->
                <input type="image"  name="submit" value="like" src="like.jpg">
                <input type="image"  name="submit" value="dislike" src="2.jpg">
            </form>
        </td>
    </tr>
    <?php
}

Thanks, I'll try it and Happy Celebration of New Year!

Same to you! :)

One thing, please watch out when the buttons should be display. If a user has already voted for (like or dislike), the buttons should not be display again (and the server should still know that the user has already voted and will ignore any other incoming vote from the same user), or a user would be able to vote multiple times.

Yes, I know, It should check whether definite user has voted, and in my project, if he is liked, he should have choice to dislike it. I didn't know how to put it in input button in table and also have to add it, too.. :)

I put that form in my project and buttons works now. But what have to do in submit code, so that when user votes with like/dislike this to be for definite song? I saw and use your code:

<input type="hidden" name="song_id" value="<?php echo $row['song_id']; ?>">

But in my code for buttons, what have to write in place of song_id? As I've written it ('".$row['song_id']."') it does not work.

if (isset($_POST['submit'])) {
    if ($_POST['submit'] == 'like') {
        $insert = mysqli_query($mysqli, "INSERT INTO `voting` (`user_id`,`song_id`,`likes`) VALUES ('".$_SESSION['user_info']['user_id']."','".$row['song_id']."', 1)");
    }

    if ($_POST['submit'] == 'dislike') {
        $insert2 = mysqli_query($mysqli, "INSERT INTO `voting` (`user_id`,`song_id`,`dislikes`) VALUES ('".$_SESSION['user_info']['user_id']."','',1)");
    }
    }

Well, I guessed that you wanted to know the ID of the song for which the user is voting, so I thought: let's pass that data to the action page. I think you should use the data in $_POST['song_id'] instead of the data in $row['song_id'], which doesn't exist any more when the form is submitted; only the POST data is sent with the request.

Thank you :) Now it's ok.

Hello again! In my table, there should be seen likes and dislikes from Database.
I try with that code, but there is a warning: Warning: Illegal offset type in...

<?php echo $row['janr_name']; ?>
    </th>
    <th>
    <?php echo $row[($query = mysqli_query($mysqli,"SELECT SUM(`likes`) FROM `voting` WHERE `song_id`=1"))]; ?>
    </th>
    <th>

What's wrong in this query? :)

There's nothing wrong with the query, there's something wrong with your PHP: mysqli_query() does not return the results directly; it returns a query resource kind of thing. You still need to fetch the results using (for example) mysqli_fetch_assoc().

Yes, I did it properly this time. I made one more query and combined it with the main query. After that I used mysqli_fetch_array().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.