首页 > 解决方案 > 如何一键同时编辑和添加(PHP)

问题描述

我的 sql 代码有问题。如果使用一个字段,我设法在一个保存按钮中创建添加和编辑功能,但如果我添加更多字段则失败。我可以完美地添加它,但无法编辑该字段。

我的情况是我需要添加经验,如果经验是空的,我需要添加新的经验,或者如果我有添加之前我可以添加更多经验并可以编辑它。

这是我的经验形式的代码

      <form class="form-horizontal form-label-left" method="post" >

                       <div class="form-group">
                    <div align="center">
               <table align="center" cellspacing="0">
                       <tr>
                         <td><strong>Accreditation Body </strong></td>
                         <td><strong for="institution">Type of Auditor</strong></td>
                         <td><div align="center"><strong>Year of Experience (example : 2000-2002)</strong></div></td>
                         <td></td>
                         </tr>
                       <?php
                         $i = 0;

                          foreach ($exps as $exp)
                          // foreach (array_slice($exps,1) as $exp)
                         {?>
                             <tr>
                         <td>  <textarea id="description" required="required" name="description[example<?php echo ++$i;?>]" class="form-control col-md-7 col-xs-12"><?php echo $exp['description'];?></textarea></td>
                         <td> 

                          <select class="form-control" type="" id="institution" name="institution[example<?php echo ++$i;?>]" required>
                              <option></option>
                              <option value="External Auditor" <?php echo helpers::selectValueEdit("External Auditor", "institution[]", $exp['institution']);?>>External Auditor</option>
                                <option value="Internal Auditor" <?php echo helpers::selectValueEdit("Internal Auditor", "institution[]", $exp['institution']);?>>Internal Auditor</option>
                              </select> 


             </td>
                         <td> 


                          <input type="text" id="year" name="year[example<?php echo ++$i;?>]" required="required" class="form-control col-md-7 col-xs-12" value="<?php echo $exp['year'];?>">
                          </td>

                                 <input type="hidden" name="id[example<?php echo $i;?>]" value="<?php echo $exp['experience_id'];?>"/td>

                        <td>


      </form>

<script>
        <?php
        $input = '\' 
        <div class="control-group input-group" style="margin-top:10px">
                <div align="right_col">
                  <table align="" cellspacing="0">
                  
                   
                      <tr>

                        <td>  <textarea id="description" type="text" required="required" name="description[]" class="form-control "></textarea></td>
                          
                      <td>

                      <select class="form-control"  type="text" id="institution" name="institution[]"  required>
                      <option></option>
                                   <option value="External Auditor">External Auditor</option>
                                    <option value="Internal Auditor">Internal Auditor</option>
                                  </select>
                        </td>
                        <td>  <input type="text" id="year" name="year[]" required="required" class="form-control" >
                        </td>
                        <td>
 <div class="input-group-btn"> 
              <button class="btn btn-danger remove" type="button"><i class="glyphicon glyphicon-remove"></i> Remove</button>
            </div>

                        </td>
                      </tr>

             
                     <input type="" name="txt_holder" id="txt_holder" hidden> 


                  </table>
</div>

              </div>\'';

        $input = preg_replace( "/\r|\n/", "", $input );
        ?>
        function addNew() {

            $("#insert-after").append(<?php echo $input;?>);

            if ($("#edit-button").hasClass("hidden")){
                $("#edit-button").removeClass("hidden");
            }
        }


    </script>
<!-- page content -->
        <div class="right_col" role="main">
          <div class="">
            <div class="page-title">
              <div class="title_left">
                <h3>Edit Profile</h3>
              </div>

              
            </div>
            <div class="clearfix"></div>

            <div class="row">

              <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                      <?php session::display_msg();?>
                    <h2>Experience</h2>
                    
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">

          <form class="form-horizontal form-label-left" method="post" >

                           <div class="form-group">
                        <div align="center">
                   <table align="center" cellspacing="0">
                           <tr>
                             <td><strong>Accreditation Body </strong></td>
                             <td><strong for="institution">Type of Auditor</strong></td>
                             <td><div align="center"><strong>Year of Experience (example : 2000-2002)</strong></div></td>
                             <td></td>
                             </tr>
                           <?php
                             $i = 0;

                              foreach ($exps as $exp)
                              // foreach (array_slice($exps,1) as $exp)
                             {?>
                                 <tr>
                             <td>  <textarea id="description" required="required" name="description[example<?php echo ++$i;?>]" class="form-control col-md-7 col-xs-12"><?php echo $exp['description'];?></textarea></td>
                             <td> 

                              <select class="form-control" type="" id="institution" name="institution[example<?php echo ++$i;?>]" required>
                                  <option></option>
                                  <option value="External Auditor" <?php echo helpers::selectValueEdit("External Auditor", "institution[]", $exp['institution']);?>>External Auditor</option>
                                    <option value="Internal Auditor" <?php echo helpers::selectValueEdit("Internal Auditor", "institution[]", $exp['institution']);?>>Internal Auditor</option>
                                  </select> 

                              
                 </td>
                             <td> 


                              <input type="text" id="year" name="year[example<?php echo ++$i;?>]" required="required" class="form-control col-md-7 col-xs-12" value="<?php echo $exp['year'];?>">
                              </td>
                             
                                     <input type="hidden" name="id[example<?php echo $i;?>]" value="<?php echo $exp['experience_id'];?>"/td>
                                    
                            <td>


                            <a href="delete_admin.php?exp=<?php echo $exp['experience_id'];?>&p_id=<?php echo $exp['exp_id']; ?>" name="delete" class="btn btn-sm btn-danger" id="delete-button">Delete</a>



                              



                            </td>
                             </tr>

                            

                          </table>
                           <div id="insert-after"></div>
                                                                                  <a href="" onclick="addNew();return false;"  class="btn btn-success" >Add New</a>


                              <input type="submit" name="edit" value="Save" class="btn btn-success" id="edit-button">

           

          </form>

这是我的 sql:

public function update_example2()

{ $user_id = $_GET['id'];

    if ($_SERVER['REQUEST_METHOD'] == "POST")
    {
        if (!isset($_POST['id']))
        {
                for($count = 0; $count< count($description); $count++)
                {
                    $description = $_POST["description"][$count];
                    $institution = $_POST["institution"][$count];
                    $year = $_POST["year"][$count];
                    $user_id = $_GET['id']; 


                $this->database->query("INSERT INTO experiences
                                        SET description = '$description',
                                        institution = '$institution',
                                        year = '$year',
                                        exp_id = '$user_id'");

} } 别的{

            $new_array = array_merge_recursive($_POST['description'],$_POST['institution'],$_POST['year'],$_POST['id']);

            foreach ($new_array as $spec)
            {


                if (is_array($spec))
                 {

                $description = $spec['0'];
                $institution = $spec['1'];
                $year = $spec['2'];
                $experience_id= $spec['3'];


                $this->database->query("UPDATE experiences
                                            SET description = '$description',
                                            institution = '$institution',
                                            year = '$year'
                                            WHERE experience_id = '$experience_id'");



                }

                else{






                for($count = 0; $count< count($description); $count++)
                {
                    $description = $_POST["description"][$count];
                    $institution = $_POST["institution"][$count];
                    $year = $_POST["year"][$count];
                    $user_id = $_GET['id']; 


                $this->database->query("INSERT INTO experiences
                                        SET description = '$description',
                                            institution = '$institution',
                                            year = '$year',
                                            exp_id = '$user_id'");

}

                }
            }
        }

        $user_id = $_GET['id'];
        if ($this->database->affected_rows() == 0 )
        {
            session::set_msg(["No changes have been made"]);
            header("Location: example.php?id=$user_id");
        exit();


        }
        else
        {

            session::set_msg("Experiences have been updated","success");
        header("Location: example.php?id=$user_id");
            exit();
    }
     }
}

我设法成功添加和编辑此表单1 需要表单的图片 - 编辑模式不成功2

标签: javascriptphp

解决方案


推荐阅读