Insert row in MYSQL without leaving the page (using PHP)

-1

I have a system of downloading songs with a download button and I would like that when I click the button, it inserts a line in the database without leaving the page.

Now what I did is open a new page that does the insert:

For each song has this code in the table row:

<form method="post" action="inseremusica.php">
<input type="text" name="id">
<button type="submit">
</form>

inseremusica.php

<?php
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO euk_lista_downloads (idmusica, data)
VALUES ($_POST['id'], date('d/m/Y H:i:s', time()))";
$conn->query($sql);
$conn->close();
?>
    
asked by anonymous 04.06.2018 / 16:23

2 answers

0

If you have the ability to use jQuery , you can do what you want by enabling an action on the click of the download, sending a POST to another PHP page, where there you INSERT , so you do not have to refresh the page in question.

    
04.06.2018 / 16:25
0

You can have a button that calls an ajax function. In this ajax function you put the command:

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "INSERT INTO euk_lista_downloads (idmusica, data)
VALUES ($_POST['id'], date('d/m/Y H:i:s', time()))";
$conn->query($sql);
$conn->close();
    
04.06.2018 / 16:26