I'm fairly new to PHP, JS and other web development oriented languages.
I have a big table that contains info directly retrieved from the database, at the end of each row I would like to add a button that deletes that line from the database.
Since there are a lot of entries in this table, I would like the button to call a function that erases the data concerning this row, but I can't seem to find how to even open a connection with the database from a script
<table>
<tr class="titles">
<td> <strong> column1 - id </strong> </td>
<td> <strong> column2 </strong> </td>
<td> <strong> column3 </strong> </td>
<td> <strong> column4 </strong> </td>
<td> <strong> etc... </strong> </td>
<td> </td>
</tr>
<?php
$query = 'SELECT * FROM my_table';
$req = $bdd -> query($query);
$n = 0;
while ($data = $req -> fetch())
{
echo '<tr>';
echo '<td>' . $data['id'] . '</td>';
echo '<td>' . $data['column2'] . '</td>';
echo '<td>' . $data['column3'] . '</td>';
echo '<td>' . $data['column4'] . '</td>';
echo '<td>' . $data['column5'] . '</td>';
echo '<td> <button type="button" id="' . $data['id'] . '" onclick=\"delete_line(this.id);\" > Delete </button> </td>';
echo '</tr>';
}
?>
</table>
<script>
function delete_line(value) {
//*fucntion that execute the query : "DELETE FROM my_table WHERE id = value LIMIT 1;
}
</script>
Should the script modify a chunk of php and then call the same page (like a form) so it's executed??