简体   繁体   English

使用Post Button更新表行值

[英]Update Table Row Value with Post Button

I have some data in my mysql database which I am displaying in table using PHP like below 我在我的mysql数据库中有一些数据,我使用PHP在表中显示如下所示

在此输入图像描述

$requirement_qry="SELECT t1.*, t2.id as unit_id, t2.name as unit_name FROM `tbl_requirements` 
t1 INNER JOIN tbl_units AS t2 on unit_type = t2.id AND project_id='".$_GET['project_id']."' AND user_id='".$userid."'";
$requirement_result=mysqli_query($mysqli,$requirement_qry);

                <form action="" name="addeditcategory" method="post" class="form form-horizontal" enctype="multipart/form-data">
            <input  type="hidden" name="project_id" value="<?php echo $_GET['project_id'];?>" />

          <div class="section">
            <div class="section-body">
              <div class="form-group">
                <label class="col-md-3 control-label" >Project Name :-</label>
                <div class="col-md-6">
                  <input type="text" name="name" id="name" value="<?php if(isset($_GET['project_id'])){echo $row['name'];}?> " class="form-control" disabled>
                </div>
              </div>

              <div class="form-group">
                <label class="col-md-3 control-label" >Location :-</label>
                <div class="col-md-6">
                  <input type="text" name="location" id="location" value="<?php if(isset($_GET['project_id'])){echo $row['location'];}?> " class="form-control" disabled>
                </div>
              </div>


              <div class="form-group">
                <label class="col-md-3 control-label">Project Status :-</label>

                <div class="col-md-6">

                          <select name="status" id="status" class="select2" disabled>
                            <?php if (!isset($_GET['project_id'])) { ?>
                            <option value="1">--Project Status--</option>
                            <?php } ?>
                            <option value="1" <?php echo $row['status'] == '1' ? 'selected' : ''; ?> >Open</option>                            
                            <option value="2" <?php echo $row['status'] == '2' ? 'selected' : ''; ?> >Closed</option>                            
                        </select>

                </div>
              </div>



               <div class="form-group">
                <div class="col-md-3">
                  <label class="control-label">Project Details :-</label>
                </div>
                <div class="col-md-6">
                  <textarea  name="details" id="details" rows="4" class="form-control" disabled><?php echo stripslashes($row['details']);?></textarea>

                </div>
              </div>


                <div class="form-group">
                <div class="col-md-3">
                  <label class="control-label">Project Requirements :-</label>
                </div>
                <div class="col-md-6">
                  <table id="t01">
                    <thead>
                      <tr>
                        <th>#</th>
                        <th>Requirements</th> 
                        <th>Required</th>
                        <th>Sent</th>
                        <th>Action</th>
                      </tr>
                    </thead>
                    <tbody>
                            <?php
                            $no     = 1;
                            while ($row1 = mysqli_fetch_array($requirement_result))
                            {
                                $id = $row1['id'];
                                $unit_name = $row1['unit_name'];
                                echo '<input  type="hidden" name="reqId" id= "reqId" value="'.$id.'" />';
                                echo '<tr>
                                        <td>'.$no.'</td>
                                        <td>'.$row1['name'].'</td>
                                        <td>'.$row1['unit_required']." ".$unit_name.'</td>
                                        <td><input type="number" id = "received" name = "received" value ="'.$row1['unit_received'].'"/></td>
                                        <td><button type="submit" name="submit" class="btn btn-primary" style="padding:5px 10px;">Submit</button></td>
                                    </tr>';
                                $no++;
                            }?>
                    </tbody>
                    </table>

                </div>
            </div>
          </div>
        </form>

Above code is properly displaying my data. 上面的代码正确显示我的数据。 I have one field value from row need to update using submit button. 我需要使用提交按钮更新行中的一个字段值。 Its working fine if there only one row...if there multiple row, its not updating data of it except last row. 如果只有一行,它的工作正常...如果有多行,它不会更新它的数据,除了最后一行。 My submit code is like below 我的提交代码如下所示

if(isset($_POST['submit']) and isset($_POST['project_id']))
{       

        $projectId = $_GET['project_id'];

        $data = array(
                     'unit_received'  =>  $_POST['received']
                    );      
                 $unit_edit=Update('tbl_requirements', $data, " WHERE id = '".$_POST['reqId']."'");
                 print_r($unit_edit);
                 echo $unit_edit;


        if ($unit_edit > 0)
        { 

                    $_SESSION['msg']="11"; 
                    header( "Location:view_open_project.php?project_id=".$_POST['project_id']);
                    exit;

        }
}

I am little new in PHP, Let me know if someone can help me for solve the bug. 我是PHP的新手,让我知道是否有人可以帮助我解决这个问题。 Thanks a lot :) 非常感谢 :)

As @Saji has already stated, you are replicating your name through the loop.You should rather use 正如@Saji已经说过的那样,你正在通过循环复制你的名字。你应该使用

echo '<input  type="hidden" name="reqId[]" id= "reqId" value="'.$id.'" />';

Note the [] brackets that were appended so that you create an array of names. 请注意附加的[]括号,以便创建名称数组。 To update, you need a loop like 要更新,你需要一个循环

for($i=0;$i<count($_POST['reqId']);$i++){
    $unit_edit=Update('tbl_requirements', $data, " WHERE id = '".$_POST['reqId'][$i]."'");
}

What you can do is just create a hidden form where you will keep original elements. 你可以做的只是创建一个隐藏的表单,你将保留原始元素。 When the save button click you just find the actual value and set this to the elements inside hidden form, then trigger the submit button inside hidden form. 单击保存按钮时,只需找到实际值并将其设置为隐藏表单中的元素,然后触发隐藏表单中的提交按钮。

Note the changes I have done on your code. 请注意我对您的代码所做的更改。

1.Removed the hidden element reqId from inside loop. 1.从内部循环中reqId隐藏元素reqId

2.Given a class name to the button inside loop and changed the button type from submit to button . 2.给循环内的按钮指定一个类名,并将按钮类型从submit更改为button Added data attribute data-id="'.$id.'" . 添加了数据属性data-id="'.$id.'"

3.The input received inside the loop has given unique id and name by concatenated the $id . 3.循环内部received输入通过连接$id给出了唯一的id和名称。

4.Created hidden form with hidden input in it. 4.处理隐藏的表格,隐藏输入。 It has your actual input names. 它有你的实际输入名称。

5.Created a jquery function to attach click event to the button inside loop. 5.处理一个jquery函数,将click事件附加到循环内部的按钮。

6.Moved the hidden element project_id to inside hidden form. 6.将隐藏元素project_id到隐藏表单内部。

Now see the below code for more details. 现在查看以下代码以获取更多详细信息。 Hope this will help you.. 希望这个能对您有所帮助..

 <form action="" name="addeditcategory" method="post" class="form form-horizontal" enctype="multipart/form-data">
                    <input type="hidden" name="project_id" value="<?php echo $_GET['project_id']; ?>"/>

                    <div class="section">
                        <div class="section-body">
                            <div class="form-group">
                                <label class="col-md-3 control-label">Project Name :-</label>
                                <div class="col-md-6">
                                    <input type="text" name="name" id="name" value="<?php if (isset($_GET['project_id'])) {
                                        echo $row['name'];
                                    } ?> " class="form-control" disabled>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-3 control-label">Location :-</label>
                                <div class="col-md-6">
                                    <input type="text" name="location" id="location" value="<?php if (isset($_GET['project_id'])) {
                                        echo $row['location'];
                                    } ?> " class="form-control" disabled>
                                </div>
                            </div>


                            <div class="form-group">
                                <label class="col-md-3 control-label">Project Status :-</label>

                                <div class="col-md-6">

                                    <select name="status" id="status" class="select2" disabled>
                                        <?php if (!isset($_GET['project_id'])) { ?>
                                            <option value="1">--Project Status--</option>
                                        <?php } ?>
                                        <option value="1" <?php echo $row['status'] == '1' ? 'selected' : ''; ?> >Open</option>
                                        <option value="2" <?php echo $row['status'] == '2' ? 'selected' : ''; ?> >Closed</option>
                                    </select>

                                </div>
                            </div>


                            <div class="form-group">
                                <div class="col-md-3">
                                    <label class="control-label">Project Details :-</label>
                                </div>
                                <div class="col-md-6">
                                    <textarea name="details" id="details" rows="4" class="form-control"
                                              disabled><?php echo stripslashes($row['details']); ?></textarea>

                                </div>
                            </div>


                            <div class="form-group">
                                <div class="col-md-3">
                                    <label class="control-label">Project Requirements :-</label>
                                </div>
                                <div class="col-md-6">
                                    <table id="t01">
                                        <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Requirements</th>
                                            <th>Required</th>
                                            <th>Sent</th>
                                            <th>Action</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $no = 1;
                                        while ($row1 = mysqli_fetch_array($requirement_result)) {
                                            $id = $row1['id'];
                                            $unit_name = $row1['unit_name'];
                                            echo '<tr>
                                                        <td>' . $no . '</td>
                                                        <td>' . $row1['name'] . '</td>
                                                        <td>' . $row1['unit_required'] . " " . $unit_name . '</td>
                                                        <td><input type="number" id = "received' . $id . '" name = "received' . $id . '" value ="' . $row1['unit_received'] . '"/></td>
                                                        <td><button type="button" name="submit" class="btn btn-primary submit-click" data-id="' . $id . '" style="padding:5px 10px;">Submit</button></td>
                                                    </tr>';
                                            $no++;
                                        } ?>
                                        </tbody>
                                    </table>

                                </div>
                            </div>
                        </div>
                </form>

                <form action="" id="addeditcategory_temp" name="addeditcategory_temp" method="post" class="form form-horizontal" enctype="multipart/form-data" style="display:none;>
                    <input type="hidden" name="project_id" value="<?php echo $_GET['project_id']; ?>"/>
                    <input  type="hidden" name="reqId" id= "reqId" value="" />
                    <input type="number" id = "received" name = "received" value ="">
                    <button id="submitButton" type="submit" name="submit" class="btn btn-primary" style="padding:5px 10px;">

                </form>

                <script>

                    $(document).ready(function(e){
                        $('.submit-click').off('click').on('click', function(e){
                            var reqId = $(this).data('id');
                            var received =  $('#received'+reqId).val();
                            var form = $('#addeditcategory_temp');
                            form.find('#reqId').val(reqId);
                            form.find('#received').val(received);
                            form.find('#submitButton').trigger('click');
                        })
                    });
                </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM