Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果要在PHP中编辑行,请更新查询插入_Javascript_Php_Jquery_Mysql - Fatal编程技术网

Javascript 如果要在PHP中编辑行,请更新查询插入

Javascript 如果要在PHP中编辑行,请更新查询插入,javascript,php,jquery,mysql,Javascript,Php,Jquery,Mysql,我创建了一个页面,可以轻松地更新或插入数据。它实际上是有效的,但是如果我点击编辑按钮,然后关闭模式,然后添加新数据,它会更新以前的数据,而不是插入新行。但是如果我刷新一个页面,然后我更新一行,它就会工作。我怎样才能解决这个问题 index.php: <form method="post" id="insert_form"> <label><b>Name:</b></label> <input type="text" nam

我创建了一个页面,可以轻松地更新或插入数据。它实际上是有效的,但是如果我点击编辑按钮,然后关闭模式,然后添加新数据,它会更新以前的数据,而不是插入新行。但是如果我刷新一个页面,然后我更新一行,它就会工作。我怎样才能解决这个问题

index.php

<form method="post" id="insert_form">
  <label><b>Name:</b></label>
  <input type="text" name="name" id="name" class="form-control" readonly required />
  <br />
  <label><b>Description:</b></label>
  <input type="text" name="hu" id="hu" class="form-control"></textarea>
  <br />
  <label><b>Cégek:</b></label>
  <select name="company[]" id="company" multiple>
  <?php
      while($row = $huResult2->fetch_array()) {
  ?>
      <option value="<?php echo $row['company_id'];?>"><?php echo $row['company_name'];?></option>
  <?php
      }
  ?>
  </select>
  <br/><br/>
  <input type="hidden" name="data_id" id="data_id" />
  <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />
</form>

<script>
  $('#add').click(function() {
    $('#insert').val("Insert");
    $('#insert_form')[0].reset();
    $('#company').multiselect('refresh');
    $('#name').removeAttr('readonly');
  });

  // Edit roles
  $(document).on('click', '.edit_data', function() {
    $("#company option").prop("selected", false);
    $("#name").attr('readonly', true);
    var data_id = $(this).attr("id");

    // Receive the current datas for the roles
    $.ajax({
      url: "fetchRole.php",
      method: "POST",
      data: {
        'data_id': data_id
      },
      dataType: "json",
      success: function(data) {
        $('#name').val(data.name);
        $('#hu').val(data.hu);
        $.each(data.companies, function(i, e) {
          $("#company option[value='" + e + "']").prop("selected", true);
        });
        $('#company').multiselect('refresh');

        $('#data_id').val(data.id);
        $('#insert').val("Update");
        $('#add_data_Modal').modal('show');
      }
    });
  });

  $('#insert_form').on("submit", function(e) {
    e.preventDefault();

    // Update and insert
    $.ajax({
      url: "insertRole.php",
      method: "POST",
      data: $('#insert_form').serialize(),
      beforeSend: function() {
        $('#insert').val("Updating...");
      },
      success: function(data) {
        $('#insert_form')[0].reset();
        $('#add_data_Modal').modal('hide');
        $('#role_table').html(data);
        location.reload();
      }
    });
  });
</script>

姓名:

说明:
塞盖克:

$('#添加')。单击(函数(){ $('插入').val(“插入”); $('#插入_形式')[0].reset(); $(“#公司”).multiselect(“刷新”); $('#name').removeAttr('readonly'); }); //编辑角色 $(文档)。在('单击','上。编辑数据',函数(){ $(“#公司期权”).prop(“选定”,false); $(“#name”).attr('readonly',true); var data_id=$(this.attr(“id”); //接收角色的当前数据 $.ajax({ url:“fetchRole.php”, 方法:“张贴”, 数据:{ “数据id”:数据id }, 数据类型:“json”, 成功:功能(数据){ $('#name').val(data.name); $('#hu').val(data.hu); 美元。每个(数据、公司、职能(即){ $(“#公司期权[价值=”+e+“]”).prop(“选定”,true); }); $(“#公司”).multiselect(“刷新”); $('#data_id').val(data.id); $('插入').val(“更新”); $('add_data_Modal').Modal('show'); } }); }); $(“#插入#表格”)。在(“提交”,功能(e){ e、 预防默认值(); //更新和插入 $.ajax({ url:“insertRole.php”, 方法:“张贴”, 数据:$('#insert_form')。序列化(), beforeSend:function(){ $('#insert').val(“更新…”); }, 成功:功能(数据){ $('#插入_形式')[0].reset(); $('#add_data_Modal').Modal('hide'); $('#角色表').html(数据); location.reload(); } }); });
关闭模式时,请做一件事,使
数据\u id
字段值为空

 $('#myModal').on('hidden.bs.modal', function () {
  $("#data_id").val("");
 })
也许会有帮助

或者单击“添加新代码”按钮,执行相同操作

$('#add').click(function() {
    $('#insert').val("Insert");
    $('#insert_form')[0].reset();
    $('#company').multiselect('refresh');
    $("#data_id").val("");
    $('#name').removeAttr('readonly');
  });

可能在您的
insertRole.php
文件中,您正在根据
data\u id
检查条件。请通过打印签入该文件,您在所有场景(插入、更新)中都能得到正确的结果。insertRole.php文件很好。我的问题是,如果在关闭编辑模式后添加新行,则会更新该行,而不是插入新行。但是如果我刷新页面,然后添加一个新行,它就工作了。