my question is, how could I insert data from a table with the click of a button into mysql DB?
The table on my HTML is as follows:
<table>
<tr>
<th>Name</th>
<th>Number</th>
</tr>
<tr>
<td>data</td>
<td>data</td>
</tr>
</table>
The rows of course continue, depending on the data inserted.
In my DB, the tables are as follows:
Person (ID, Name, Number)
The number table in the DB is blank, while the Name and ID tables are already filled up with data.
I have a global variable where the number of total people is stored, which I think can come in handy
var totalPeople
As I mentioned before, I would like to make an ajax request and send the "Number" data from the HTML table into the Number column inside my DB, matching the name each person has (So, if for example, on my HTML table I have "David" with number 3 and "James" with number 5, I want the button to insert 3 into the David row in my DB and 5 into the James row).
I thought of creating an array from the table and sending that array through ajax and making the SQL query, but I'm quite stuck as to how to do this...
Thanks in advance
Well, here is how you should do, in my opinon:
persons_table.php: the page where you display your data table with hidden inputs with persons ids, example (use loops of course):
<form method="post" action="save_persons.php">
<table>
[headers...]
<tr>
<td><input type="hidden" name="id[]" value="<?= $id ?>" /> John</td>
<td><input type="text" name="number[]" value="<?= $number ?>" /> </td>
</tr>
</table>
[submit button...]
</form>
save_persons.php: the page which gonna be requested, you can loop on $_POST (I suppose you're doing this way) and UPDATE your rows like this:
UPDATE Person SET number = $number WHERE id = $id;
I would suggest you to have a look at PDO, prepared statements and Model View Controller design pattern.
Regards.