I am pulling records from a MySQL table and output them in a table on my webpage. Now I am in need of being able to edit the records which I tried to accomplish by placing an edit button at the end of each row, which is then supposed to open the record details in my bootstrap modal.
Sadly, this doesnt work as the passed on variables are all for the last entry of the queried records. How do I create a modal link that addresses the record of the specified row?
Here what I have so far.
Query:
<?php
// Start MySQLi connection
include '../../plugins/MySQL/connect_db.php';
$db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
// Build basic query
$sql = ("SELECT CRS, ROOM, ARR, DEP, NTS, TITLE, FIRST, LAST, NAT, IHG_LVL, EMAIL FROM qci_poststay_ADMIN_temp");
// run the query or show an error message
if(!$result = $db->query($sql)){
echo('There was an error running the query [' . $db->error . ']');
}
while($row = mysqli_fetch_array($result)){
$crs = $row['CRS'];
$room = $row['ROOM'];
$arrival = $row['ARR'];
$departure = $row['DEP'];
$nights = $row['NTS'];
$nationality = $row['NAT'];
$title = $row['TITLE'];
$first = $row['FIRST'];
$last = $row['LAST'];
$lvl = $row['IHG_LVL'];
$email = $row['EMAIL'];
echo "
<tr>
<td id=\"crs\">$crs</td>
<td>$room</td>
<td>$arrival</td>
<td>$departure</td>
<td>$nights</td>
<td>$nationality</td>
<td>$title</td>
<td>$first</td>
<td>$last</td>
<td>$lvl</td>
<td>$email</td>
<td>00</td>
<td>yyyy-mm-dd</td>
<td>
<a id=\"Send_Mail\" class=\"btn btn-block btn-primary btn-xs\" target=\"_blank\" href=\"./sendmail.php?crs=$crs\" method=\"POST\">Send</a>
<button type=\"button\" class=\"btn btn-block btn-warning btn-xs\" data-toggle=\"modal\" data-target=\"#edit\">Edit</button>
</td>
</tr>";
}
//$result->free();
$db->close();
?>
Modal:
<!-- Modal -->
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Edit Details</h4>
</div>
<div class="modal-body">
<table class="table">
<tr>
<?php
echo "
<td>CRS No.: </td><td>$crs</td>";
?>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- /.Modal -->
Much appreciate some help on this. Thanks!