I have hound_view.php which displays the records based on search/filter criteria. When a user clicks on a row, a modal is displayed with a form for editing the hound info, which I want to be pre-populated based on the row that was clicked.
When a row in hound_view.php is clicked hound_record_click_event.js sends it's data-id to fill_hound_records.php where the database is queried and each row of data is assigned a variable to then pass to the modal in edit_hound_records.php
My issue is that when I click on any row, I am only getting the very first row's data to populate, but in fill_hound_records.php I echo back the variable to be sure each row is capturing its correct info and it is.
Please Note: This code is vulnerable to SQL injection attacks and should not be copied directly. You should use mysqli or PDO prepared statements with bound parameters as described in this post.
hound_view.php
<tbody class="clickable-row" data-toggle="modal" data-target="#editModal" data-id="<?php echo $row['RegNumber'] ?>">
hound_record_click_event.js
$(window).ready(function() {
//bind the event using jquery not the onclick attribute of the button
$('.clickable-row').on('click', updateClick);
});
function updateClick() {
var dataid = $(this).data("id");
$.ajax ({
type: 'POST',
url: "fill_hound_record.php",
data: { dataid : dataid },
success: function( result ) {
alert(result);
}
});
};
fill_hound_record.php
<?php
//include database configuration file
include('db.php');
$hounddataid = $_POST["dataid"];
//get rows
$prefillsql = "SELECT * FROM Hounds WHERE RegNumber = '$hounddataid'";
$prefillquery = mysqli_query($con,$prefillsql)or die(mysql_error());
$prefillnumrows = mysqli_num_rows($prefillquery);
if($prefillnumrows > 0){
while($row = mysqli_fetch_array($prefillquery)){
$prefillbreed = $row['Breed'];
$_SESSION['pfbreed'] = $prefillbreed;
$prefillregnumber = $row['RegNumber'];
$_SESSION['pfregnumber'] = $prefillregnumber;
echo $_SESSION['pfregnumber'];
}}
?>
edit_hound_record.php
<input type="text" class="form-control" id="regnumber" name="regnumber" placeholder="Reg Number" value="<?php echo $_SESSION['pfregnumber']; ?>">