0

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? :)
3
Contributors
18
Replies
63
Views
2 Years
Discussion Span
Last Post by iveto89
0

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?

0
    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.
0

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> 
0

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").

0

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?

0

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.

0

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>"; 
}
0

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
}

Edited by minitauros

0

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.

0

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.. :)

0

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)");
    }
    }
0

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.

0

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? :)

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.