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

Asked

Viewed 80 times

-1

I have a music download system with a download button and I would like when clicking the button, it insert a line in the database, without leaving the page.

Currently what I did is it open a new page that makes the Insert:

For each song have this code in the table row:

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

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();
?>
  • Make a call Ajax: https://answall.com/questions/190816/como-executar-uma-fun%C3%A7%C3%A3o-php-no-ajax

2 answers

0

If you have the possibility to use jQuery, it is possible to do what you want by enabling an action at the click of the download by sending a POST to another page PHP, where there you do the INSERT, thus not having to update the page in question.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.