Reputation: 123
Good day! I got a error that says "Undefined variable: id".
I created a table which has a delete button in each row, when clicked pops up an modal and asks if I want to delete the row. But when ever I click on the delete button it says the error Undefined variable: id and doesn't delete the data.
Here's my table code:
if(isset($_POST['delete'])) {
$data->delete_multiple($id, $conn);
}
<table class="table table-hover table-bordered" id="table1">
<thead>
<tr>
<th>Faculty Code</th>
<th>Last Name</th>
<th>First Name</th>
<th>Middle Name</th>
<th>Position</th>
<th></th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while ($row = $result -> fetch_object()): ?>
<tr>
<td><?php echo $row->Faculty_ID;?></td>
<td><?php echo $row->Faculty_Lastname ?></td>
<td><?php echo $row->Faculty_Firstname ?></td>
<td><?php echo $row->Faculty_Middlename?></td>
<td><?php echo $row->Position; ?></td>
<td class="text-center">
<a class="btn btn-sm btn-outline-primary text-muted" href="Faculty_edit.php?Faculty_ID=<?php echo $row->Faculty_ID;?>">Edit</a>
<a type="button" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal" class="btn btn-sm btn-outline-danger" >Delete</button></td>
<td>
<?php if($row->Status=='Active') echo '<a href="#deactive_account" data-toggle="modal" data-id='.$row->Faculty_ID.' class="activate" style="color:green;">Active</a>'; ?>
<?php if($row->Status=='Inactive') echo '<a href="#active_modal" data-toggle="modal" data-id='.$row->Faculty_ID.' class="activate" style="color:red;">Inactive</a>'; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
My modal code:
<div class="modal fade" id="delete_modal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Delete Confirmaiton</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="alert alert-danger" role="alert">
Are you sure you want to delete selected record?
</div>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-danger" name="delete">Yes</button>
</div>
</div>
</div>
</div>
Delete function code:
function delete_multiple($id, $conn){
$id = $_POST['delete'];
$sql = "DELETE FROM faculty WHERE Faculty_ID=? ";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
}
Upvotes: 1
Views: 1778
Reputation: 187
That's because you are not passing the row id to the modal popup.
Add the faculty id to your delete button and add a unique class to it 'delete-faculty-btn'. Ref. to below line
<a type="button" data-fid="<?php echo $row->Faculty_ID;?>" title="Delete Faculty" data-toggle="modal" data-target="#delete_modal" class="btn btn-sm btn-outline-danger delete-faculty-btn" >Delete</button></td>
Change your modal popup footer to:-
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<input type="hidden" name="delete" value="" id="row-id-to-delete" />
<button type="submit" class="btn btn-danger" >Yes</button>
</div>
And add the below Javascript Script:-
$(document).on('click', '.delete-faculty-btn', function(){
$("#row-id-to-delete").val($(this).data('fid'));
});
My assumption is that you have the submit button in your modal popup inside a HTML form.
Upvotes: 2