使用php在TABLE中编辑数据

时间:2018-01-03 09:45:11

标签: php mysqli

**我想使用模态编辑我的表中的数据,但我只能调用ID = 1我无法编辑其他人,我还需要刷新我的网页才能看到更改?

//我的更新代码

    <?php
    $id= "1";
    $mysqli = new mysqli('127.0.0.1','root','','books');
    $query = $mysqli->query("select * from bookrecords where ID='$id' ");
    $row = $query->fetch_assoc();
    if(isset($_POST['update'])) {
      $id = $_POST['id'];
      $nm = $_POST['nm'];
      $is = $_POST['is'];
      $pb = $_POST['pb'];
      $au = $_POST['au'];
      $result = $mysqli->query("UPDATE bookrecords set BookName='$nm', ISBN='$is', Publisher='$pb', Author='$au' where ID='$id'");
      if($result){ 
        ?>  

    <div class="alert-success" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="close"><span aria-hidden="true">&times</span></button>
      <strong>Success!</strong> your data has been updated.
      </div>

      <?php
    } else{
    ?>

     <div class="alert-failed" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="close"><span aria-hidden="true">&times</span></button>
      <strong>Failed!</strong> Error updating. Try Again!
      </div>


       <?php
       }
     }
       ?> 

**这是我的模态表格....     

        <div class="form-group">
          <label for="id">ID:</label>
          <input type="text" class="form-control" id="id" name="id" value="<?php echo $row['ID']?>" readonly>
        </div>

        <div class="form-group">
          <label for="nm">BookName:</label>
          <input type="text" class="form-control" id="nm" name="nm" value="<?php echo $row['BookName']?>">
        </div>

        <div class="form-group">
          <label for="is">ISBN:</label>
          <input type="text" class="form-control" id="is" name="is" value="<?php echo $row['ISBN']?>">
        </div>

        <div class="form-group">
          <label for="pb">Publisher:</label>
          <input type="text" class="form-control" id="pb" name="pb" value="<?php echo $row['Publisher']?>">
        </div>

        <div class="form-group">
          <label for="au">Author:</label>
          <input type="text" class="form-control" id="au" name="au" value="<?php echo $row['Author']?>">
        </div>
        <button type="submit" name="update" class="btn-primary">Update</button>
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </form>

  </div>
  <div class="modal-footer">

  </div>
</div>

**那么如何调用表格中的任何项目/数据?请帮帮我。

1 个答案:

答案 0 :(得分:0)

您的HTML表单必须有一个隐藏字段,其中包含book记录的id,当按下“Update”按钮时,该字段将发送到PHP脚本。隐藏字段应如下所示:

<input type="hidden" name="bookrecordId" value="ID_OF_BOOKRECORD_HERE" />

您可以在接收PHP脚本中使用$_POST['bookrecordId']访问已转移ID的值。使用它来构建一个或两个prepared statements,它将检查该行是否存在,然后运行UPDATE查询来更新值。