jQuery bring value from table Mysql

0

I am putting together a list of music lists for a karaoke site. It works like this:

Search for pre-recorded music Get this input song and insert it into a li list

I have the following:

<input id="musica" type="text">
<button type="button" id="add">Adicionar</button>
<ul id="listademusicas">
</ul>

<script>
jQuery('#add').click(function() {
jQuery('#listademusicas').append('<li id="">'+jQuery('#musica').val()+'</li>');
});
</script>

Then I wanted to insert the li, it would bring the id of the song, the table looks like this:

tabela_musicas
id|nome
1 |nome da musica1
2 |nome da musica2

In the input that takes the name of the song I'm using a query that has the exact name of the song, so I thought of doing something like

SELECT id FROM tabela_musicas WHERE nome = "nome da musica".

But I do not know how to do this query dynamically. I wanted the music id to be inserted in the name or id of each line.

    
asked by anonymous 09.04.2018 / 23:17

2 answers

1

To do this dynamically you will need to use Ajax.

Create a file / route that inserts and returns what was entered.

jQuery.post('linkparacadastrarMusica', {nome: nomedaNovaMusica}).done(function(data){
   console.log(data)
})

With this, you get the feedback of the new one and manipulate it in your page as you wish.

Read more about it here .

    
10.04.2018 / 00:23
0

For MySQL, you can do a select like this:

SELECT LAST_INSERT_ID();

It will return the last entered ID. This works for applications with low index of insertion. If the application is heavily used, two inserts made at the same location will have the same ID returned.

Search by name is not a good one, because it would require all songs to have different names.

    
09.04.2018 / 23:58