I am facing a weird behavior when updating sql database from php. I have a table in the database that I am trying to update with a modal. The table is simple containing three columns only. I have a button in my page that triggers a modal.The button is the following:
<a href="#update<?php echo $id;?>" class="btn btn-success btn-xs" data-toggle = "modal" data-target="#update<?php echo $id;?>"></i> Edit</a>
The $id is taken within a loop that the button is contained. So the modal is triggered and the modal code is the following:
<div id = "update<?php echo $id;?>" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<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="myModalLabel2">Edit Project Details</h4>
</div>
<div class="modal-body">
<form method = "POST" action = "update_project.php">
<input type="hidden" name="id" value="<?php echo $id;?>">
<label>Project name</label>
<input type="text" name = "name" class="form-control" value = "<?php echo $row['project_name'];?>">
<br/>
<label>Status</label>
<select name = "status" class = "form-control">
<option value = "Active">Active</option>
<option value = "Inactive">Inactive</option>
</select>
<br/>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button name = "update" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
Everything is showing correctly in there all the values are correct. When i click on save changes the update_project.php is called that contains the following code:
<?php
include('dbcon.php');
$id = $_POST['id'];
$project_name = $_POST['name'];
$status = $_POST['status'];
mysqli_query($con,"UPDATE project SET project_name='$project_name', project_status='$status' where project_id='$id'")
or die(mysqli_error($con));
echo "<script type='text/javascript'>alert('Successfully updated project details!');</script>";
echo "<script>document.location='project.php'</script>";
This is where my problem starts. I don not get any error there but the database is not updated. The weirder part is that only the last entry of the table is changed successfully, and when for example I set the last entry as inactive, it is changed and when I change another entry of the table it does not and it reverts the last entry back to active.
I have used the same code in other pages same parameters and it works fine but here it does not. What can be wrong? Why is updates the values so unexpectedly?
I have used several codes I run through the site to report any errors but none returned any error!
As an update Instead of using <?php echo $id;?>
I replaced it with <?php echo $row['project_id'];?>
with no luck. The whole code where the button is:
<?php
include 'dbcon.php';
$query=mysqli_query($con,"select * from project ORDER BY project_id ASC")or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$id=$row['project_id'];
$total=0;
$query1 = mysqli_query($con,"SELECT * FROM budget WHERE project_id='$id'");
while ($row1=mysqli_fetch_array($query1)){
$total=$total+$row1['amount'];}
?>
<tr>
<td colspan="2"><?php echo $row['project_name'];?></td>
<td><?php echo $total;?></td>
<td> </td>
<td> </td>
<td><?php echo $row['project_status'];?></td>
<td style="width:300px">
<a href="#update<?php echo $row['project_id'];?>" class="btn btn-success btn-xs" data-toggle = "modal" data-target="#update<?php echo $row['project_id'];?>"><i class = "fa fa-pencil"></i> Edit</a>
<a href="budget.php?id=<?php echo $id;?>" class="btn btn-success btn-xs" data-target="budget.php?id=<?php echo $id;?>"><i class = "fa fa-pencil"></i> Budget Category</a></td>
</tr>
<?php include 'update_project_modal.php';?>
<?php }?>
After some debugging I did i have concluded that the $id value is passed to the modal correctly (I echod it in the modal title and the value matches in the database). The problem is in the update_project.php. There the $id value changes to the last enrty of the database for some reason. I echoed it to the message and it is the last entry... It seems that the $id does not pass from the modal to the update_project.php page...