Thanks to all who responded. I got it to update with a combination of ideas from you. I've amended the code to show what's working.
To implement LIKE functionality by passing the LIKE button value to php so as to update the DB.
This displays the table, complete with a LIKE button per row:
echo '<table>
<tr>
<th>Word ID</th>
<th>User ID</th>
<th>User Name</th>
<th>Word</th>
<th>Meaning</th>
<th>Example</th>
</tr>';
foreach ($data as $row)
{
echo '<tr>';
foreach ($row as $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
echo '<td>
<form method="POST" action="'.$_SERVER["PHP_SELF"].'">
<input type="hidden" name="LIKE" value="'.$row['wordID'].'">
<input type="submit" class="btn btn-success" value="Submit">
</form>
</td>';
echo '</tr>';
}
echo '</table>';
The code to process the form submit is:
if($_POST['submit'])
{
$sql = "UPDATE vocab SET likes = likes+1 where wordID = '{$row['wordID']}'";
$stmt = $db->prepare($sql);
/* Execute */
$stmt->execute();
}
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]?>
is a sytax you use when you are NOT already in a <?php
tag. Here, you already are writing in PHP and in an echo
and you just apen again a <?php
, and another echo
in it, this makes no sense.
echo '... <form method="post" action="'.$_SERVER["PHP_SELF"].'"> ...';
is the way you want to include your php_self value.
also
<input type="submit" value="LIKE" name="<?php echo $row["wordID"]; ?></form>
should be
<input type="submit" value="LIKE" name="'.$row['wordID'].'"></form>
but you cant the search for $_POST['wordID'], search for $_POST[$row['wordID']] in a for loop.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments