I have an HTML website with a table exported from a mySQL database command. In the HTML table I have an extra column with a “like” button that I want to add 1 to the value in the “total_anime_likes” column in a different table within the database. Its a bit confusing because the HTML table is in a PHP page.
For example – if I click the button it should add 1 “like” to the total_anime_likes value in the database, but only on the row associated with the anime_id from that like button’s row. I can’t seem to figure out how to POST the ID of the anime_id in the table into my second php page so that posts it to the database.
<?php
echo "<table class='styled-table'>";
echo "<thead><tr><td>Anime ID</td><td>Anime Title</td><td>Season</td><td>Year</td><td>Studio</td><td>Like</td></tr></thead>";
while($row=mysqli_fetch_assoc($result))
{
foreach($row as $key=>$value) ${$key}=$value;
echo "<tr><td>$anime_id</td><td>$anime_title</td><td>$season</td><td>$year</td><td>$studio_name</td><td><form method='post' action='likeAnime.php'><input type='submit' name='like' value='Like'></form></td></tr>";
}
echo "</table>";
?>
I have a table which works – and the Like buttons show up in the table next to each anime. I wanted to be able to make it so when you clicked on the button (which is actually a submit input) that it posts the $anime_id to likeAnime.php so therefore, the SQL command on that page can add 1 to the total_anime_likes value
<?php
foreach($_POST as $key=>$value) ${$key}=$value;
$like = $_POST['like'];
$sql = "UPDATE anime_rankings SET total_anime_likes = total_anime_likes + 1 WHERE anime_id='$like';";
$result = mysqli_query($db_server, $sql);
if($result) echo "INSERT success!<br />"; else echo "INSERT failed!<br />";
if($result)
{
echo "<p>Data was successfully INSERTED from the database.</p>";
}
else
{
echo "<p>Your INSERT failed and here's why:</p>";
die(mysqli_error($db_server));
}
?>
Here I have the second php page “likeAnime.php”. How could I post the anime_id value so that the SQL statement above accurately inserts the “+ 1” into the correct row in the database?
I am very new to coding – sorry if this is really hard to understand or self-explanatory.
Zachary Connell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.