Hi, I need your help.
I am a newbie in PHP and my update statement is not working despite seeing all the values from the console.
My code for the bootstrap modal file:
<!----------------------------------- modal begin -------------------------------------->
<div class="modal fade" id="editmodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit SAB Homes Properties</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="form-sample" form action="updatehouse.php" method="POST">
<div class="content-wrapper">
<div class="row">
<div class="col-12 grid-margin">
<div class="card">
<div class="card-body">
<h4 class="card-title">Update Property</h4>
<div class="form-group">
<label for="exampleInputUsername1">House Name</label>
<input type="hidden" name="update_id" id="update_id"/>
<input type="text" class="form-control" id="house_name" name="house_name" />
</div>
<div class="form-group">
<label for="exampleInputEmail1">Landlord</label>
<input type="text" class="form-control" id="house_landlord" name="house_landlord" />
</div>
<div class="form-group">
<label for="exampleInputPassword1">Location</label>
<input type="text" class="form-control" id="house_location" name="house_location" />
</div>
<div class="form-group">
<label for="exampleInputConfirmPassword1">Commission</label>
<input type="text" class="form-control" id="house_commission" name="house_commission" />
</div>
<div class="form-group">
<label for="exampleInputConfirmPassword1">Service Charge</label>
<input type="text" class="form-control" id="house_service_charge" name="house_service_charge" />
<input type="hidden" name="house_siku_added" id="house_siku_added"/>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="updatedata" id="updatedata">Save changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!----------------------------------- modal end ---------------------------------------->
The table that has the data:
<div class="row">
<div class="col-lg-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<h4 class="card-title">Houses List</h4>
<p class="card-description"> </p>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th width="10%">ID </th>
<th width="21%">Property Name</th>
<th width="14%">Landlord</th>
<th width="14%">Location</th>
<th width="14%">Commission</th>
<th width="14%">Service Charge</th>
<th width="14%">Registered</th>
<th width="22%">Edit</th>
</tr>
</thead>
<tbody>
<?php
$house = $connection->query("SELECT * FROM houses");
while($row=$house->fetch_assoc()):?>
<tr>
<td><?php echo $row['house_id']; ?></td>
<td><?php echo $row['house_name']; ?></td>
<td><?php echo $row['house_landlord']; ?></td>
<td><?php echo $row['house_location']; ?></td>
<td><?php echo $row['house_commission']; ?></td>
<td><?php echo $row['house_service_charge']; ?></td>
<td><?php echo $row['house_siku_added']; ?></td>
<td><button type="button" class="btn btn-success editbtn">Edit House</button></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php
$pr_query = "select * from houses ";
$pr_result = mysqli_query($connection,$pr_query);
$total_record = mysqli_num_rows($pr_result );
$total_page = ceil($total_record/$num_per_page);
if($page>1)
{
echo "<a href='listhouses.php?page=".($page-1)."' class='btn btn-danger'>Previous</a>";
}
for($i=1;$i<$total_page;$i++)
{
echo "<a href='listhouses.php?page=".$i."' class='btn btn-primary'>$i</a>";
}
if($i>$page)
{
echo "<a href='listhouses.php?page=".($page+1)."' class='btn btn-danger'>Next</a>";
}
?>
</div>
</div>
</div>
</div>
</div>
The script used to open the modal.
<script>
$(document).ready(function() {
$('.editbtn').on('click', function(){
$('#editmodal').modal('show');
$tr = $(this).closest('tr');
var data = $tr.children("td").map(function(){
return $(this).text();
}).get();
console.log(data);
$('#update_id').val(data[0]);
$('#house_name').val(data[1]);
$('#house_landlord').val(data[2]);
$('#house_location').val(data[3]);
$('#house_commission').val(data[4]);
$('#house_service_charge').val(data[5]);
$('#house_siku_added').val(data[6]);
});
});
</script>
This is the data passed:
The php file:
<?php
include('config/db_connect.php');
if(isset($_POST['updatedata']))
{
$house_id = mysqli_real_escape_string($connection, $_REQUEST['$update_id']);
$house_name = mysqli_real_escape_string($connection,$_REQUEST['$house_name']);
$house_landlord = mysqli_real_escape_string($connection, $_REQUEST['$house_landlord']);
$house_location = mysqli_real_escape_string($connection, $_REQUEST['$house_location']);
$house_commission = mysqli_real_escape_string($connection, $_REQUEST['$house_commission']);
$house_service_charge = mysqli_real_escape_string($connection, $_REQUEST['$house_service_charge']);
$house_siku_added = mysqli_real_escape_string($connection, $_REQUEST['$house_siku_added']);
$sql = "UPDATE houses SET house_name = '$house_name', house_landlord = '$house_landlord', house_location = '$house_location', house_commission = '$house_commission', house_service_charge = '$house_service_charge', house_siku_added = now() WHERE house_id = '$house_id'";
$sqlQuery = mysqli_query($connection, $sql);
if($sqlQuery){
echo '<script> alert("Record was updated successfully."); </script>';
header("Location:listhouses.php");
}
else
{
echo '<script> alert("Data not updated"); </script>';
}
}
?>
I would really appreciate if someone would help me see where I am going wrong with the update statement. Thank you.