douyao3895 2018-11-20 11:56
浏览 44

SQL Update表现得很奇怪

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>&nbsp;</td>
    <td>&nbsp;</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>
 &nbsp;
<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...

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值