简体   繁体   中英

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

在此输入图像描述

$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. Thanks a lot :)

As @Saji has already stated, you are replicating your name through the loop.You should rather use

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.

2.Given a class name to the button inside loop and changed the button type from submit to button . Added data attribute data-id="'.$id.'" .

3.The input received inside the loop has given unique id and name by concatenated the $id .

4.Created hidden form with hidden input in it. It has your actual input names.

5.Created a jquery function to attach click event to the button inside loop.

6.Moved the hidden element project_id to inside hidden form.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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