从数据库中获取特定值并将其显示在另一页的文本框中

时间:2018-06-11 04:46:10

标签: php

当我点击entries.php中第一个条目的编辑按钮时,我会重定向到editentries.php页面。在那里,我想只在该页面上显示第一个条目的详细信息进行编辑。但是当我点击第一个条目的编辑按钮时,我看到数据库中的所有条目而不是一个特定条目。帮帮我,我该怎么做。

这是我的entries.php

 <?php
            $result01 = mysqli_query($conn,"SELECT * FROM feedetails");
        while($res=mysqli_fetch_assoc($result01))
        {
        echo '
          <tr>
            <td><input type="checkbox"></td>
            <td>'.$res["name"].'</td>
            <td>'.$res["address"].'</td>
            <td>'.$res["email"].'</td>
            <td>'.$res["phoneno"].'</td>
            <td>'.$res["subjects"].'</td>
            <td>'.$res["price"].'</td>
            <td><form method="post" action="editentries.php"><input type="submit" name="edit" class="btn btn-primary" value="Edit"></form></td>
            <td><form method="post" action="deleteentries.php"><input type="submit" class="btn btn-danger" value="Delete" ></form></td>
          </tr>';
      }
          ?>

这是editentries.php

<?php

if(isset($_POST['edit'])){

//$selectid=mysqli_query($conn,"SELECT * FROM feedetails GROUP BY id");
$selectname=mysqli_query($conn,"SELECT * FROM feedetails GROUP BY name");
$selectaddress=mysqli_query($conn,"SELECT * FROM feedetails GROUP BY address");
$selectemail=mysqli_query($conn,"SELECT * FROM feedetails GROUP BY email");
$selectphoneno=mysqli_query($conn,"SELECT * FROM feedetails GROUP BY phoneno");
$selectprice=mysqli_query($conn,"SELECT * FROM feedetails GROUP BY price");


?>

    <form>
      <div class="form-group row">
        <label for="inputEmail3" class="col-sm-2 col-form-label">Applicant ID:</label>
        <div class="col-sm-10">
          <select class="form-control">
          <?php  
           for($i=1;$i<=40;$i++)
           {
              echo '<option value="' . $i . '">' . $i . '</option>';  
           }

           ?>
          </select>
        </div>
      </div>
      <div class="form-group row">
        <label for="inputEmail3" class="col-sm-2 col-form-label">Name:</label>
        <div class="col-sm-10">
          <?php  while($res=mysqli_fetch_array($selectname)){
            echo '<input type="text" class="form-control" id="inputEmail3" placeholder="'.$res['name'].'" >';
          }
           ?>
        </div>
      </div>
      <div class="form-group row">
        <label for="inputPassword3" class="col-sm-2 col-form-label">Address:</label>
        <div class="col-sm-10">
         <?php  while($res=mysqli_fetch_array($selectaddress)){
            echo '<input type="text" class="form-control" id="inputEmail3" placeholder="'.$res['address'].'" >';
          }
           ?>
        </div>
      </div>
      <div class="form-group row">
        <label for="inputPassword3" class="col-sm-2 col-form-label">Phone no.:</label>
        <div class="col-sm-10">
         <?php  while($res=mysqli_fetch_array($selectphoneno)){
            echo '<input type="text" class="form-control" id="inputEmail3" placeholder="'.$res['phoneno'].'" >';
          }
           ?>
        </div>
      </div>
      <div class="form-group row">
        <label for="inputPassword3" class="col-sm-2 col-form-label">Email ID:</label>
        <div class="col-sm-10">
         <?php  while($res=mysqli_fetch_array($selectemail)){
            echo '<input type="text" class="form-control" id="inputEmail3" placeholder="'.$res['email'].'" >';
          }
           ?>
        </div>
      </div>
      <div class="form-group row">
        <div class="col-sm-10">
          <button type="submit" class="btn btn-primary">Update</button>
        </div>
      </div>
    </form>

当我点击编辑按钮时,我想从数据库中获取并仅显示一个特定条目。但是我看到了数据库中的所有条目。

3 个答案:

答案 0 :(得分:1)

像这样更新html表格(entries.php)//首先你需要传递id

 <tr>
    .
    .
    .
    <td><form method="post" action="editentries.php"><input type="hidden" value="'.$res["id"].'" name="editid"><input type="submit" class="btn btn-danger" value="Edit"></form></td>
</tr>

在php editentries.php中//你需要获取id和更新条件

if(isset($_POST['edit']) && isset($_POST['editid'])){
  $id = $_POST['editid'];
  $selectname=mysqli_query($conn,"SELECT * FROM feedetails where id = $id GROUP BY name");
.
.//Same for other queries

答案 1 :(得分:0)

传递post param中的id与传递类型(编辑/删除)相同。从该ID,您可以进行查询以获取特定记录。您的查询应该是这样的:

mysqli_query($conn,"SELECT * FROM feedetails where id=".$_POST['id']);

答案 2 :(得分:0)

In the summary page do like this. 
 <?php
      $result01 = mysqli_query($conn,"SELECT * FROM feedetails");
  while($res=mysqli_fetch_assoc($result01))
  {
  echo '
    <tr>
      <td><input type="checkbox"></td>
      <td>'.$res["name"].'</td>
      <td>'.$res["address"].'</td>
      <td>'.$res["email"].'</td>
      <td>'.$res["phoneno"].'</td>
      <td>'.$res["subjects"].'</td>
      <td>'.$res["price"].'</td>
      <td><a href="editentries.php?id='.$res["id"].'"><input type="button" name="edit" class="btn btn-primary" value="Edit"></a></td>
      <td></td>
    </tr>';
  }
    ?>

In the edit page, get the id from the url and add where condition in the sql query.
$id = $_REQUEST['id'];
  $selectname=mysqli_query($conn,"SELECT * FROM feedetails where id = $id GROUP BY name");