在php中单击“编辑”按钮时,如何获取特定的行值?

在php中单击“编辑”按钮时,如何获取特定的行值?,php,html,Php,Html,下面的代码在tag_show.php中。我必须显示数据库中的所有数据,并且我正在尝试从php代码编辑数据库,因此我在html表中所有行的末尾形成了编辑按钮 echo"<br><br><br><br> <table border='6' style= 'background-color: #FFFFE0; color: #761a9b; margin: 2 auto;'> <thead> <tr>

下面的代码在tag_show.php中。我必须显示数据库中的所有数据,并且我正在尝试从php代码编辑数据库,因此我在html表中所有行的末尾形成了编辑按钮

echo"<br><br><br><br>
<table border='6' style= 'background-color: #FFFFE0; color: #761a9b; margin: 2 auto;'>
  <thead>
    <tr>

      <th>tag_title</th>
      <th>description</th>
      <th>show_in_welcome</th>
      <th>status</th>

    </tr>
  </thread>
  <tbody>";


      while($row = mysqli_fetch_assoc($get_detail))
        {
            $_SESSION['tag_title'] =$row['tag_title'];
            $_SESSION['description'] =$row['description'];
            $_SESSION['show_in_welcome'] =$row['show_in_welcome'];
            $_SESSION['status']=$row['status'];

          echo
                "<tr>
                    <td>{$_SESSION['tag_title']}</td>
                    <td>{$_SESSION['description']}</td>
                    <td>{$_SESSION['show_in_welcome']}</td>
                    <td>{$row['status']}</td>
                    <td><form action='edit.php' method='POST'><input type='hidden' name='tag_title' value='".$row["tag_title"]."'/><input type='submit' name='submit-btn' value='edit' /></form></td>

  </tr>\n";        
      }
echo“



标签标题 描述 表示欢迎 地位 "; while($row=mysqli\u fetch\u assoc($get\u detail)) { $\会话['tag\ u title']=$行['tag\ u title']; $\会话['description']=$row['description']; $会话['show_in_welcome']=$row['show_in_welcome']; $\会话['status']=$row['status']; 回声 " {$\u会话['tag\u title']} {$\u会话['description']} {$\会话['show\ in\ u welcome']} {$row['status']} \n”; }
而edit.php是

<?php

session_start();


echo"<form action='tag_show.php' method='post'>

<br><br><br>

 Tag Title<br><input name='tag_title' type='text' value='{$_SESSION['tag_title']}'><br><br>

Description <br><input name='description' type='text' value='{$_SESSION['description']}'><br><br>

Show in welcome<br><input name='show_in_welcome' type='text' value='{$_SESSION['show_in_welcome']}'><br><br>

Status<br><input name='status' type='text' value='{$_SESSION['status']}'> <br><br>

 <input name='submit1' type='submit'  value='Update'>

 </form>";

 ?>

看起来您从未使用从表单收到的信息更新会话数据。在代码的某个地方,您将需要类似这样的内容

$_SESSION['tag_title'] = $_POST['tag_title'];
$_SESSION['description'] = $_POST['description'];
$_SESSION['show_in_welcome'] = $_POST['show_in_welcome'];
$_SESSION['status']= $_POST['status'];
有些事情需要考虑。

  • 确保用户提交表单后,数据库信息不会覆盖这些值
  • 在使用用户输入之前,始终清理/验证用户输入(与上面的代码不同,因为它只是一个示例)

    • 这里的会话毫无意义。每次循环运行之前替换的值时,都会得到最后一个值。您可以使用查询字符串的概念来代替。不打印表单,而是打印锚定标记

       <a href='file.php?id='.$row["tag_title"]>Edit</a>
      

      现在使用id运行mysql查询并选择所需的数据并以表单显示

      当然,当会话数组在每次迭代后覆盖前一行的值时,您的代码将返回最后一行。 将会话数组设置为多维数组,如$\u session[$id][“tag\u title”],然后在edit.php中使用id值引用会话数组

      $_SESSION[$id] = array('tag_title' => $row['tag_title'], 'description' => $row['description'], 'show_in_welcome' => $row['show_in_welcome'], 'status' => $row['status'])
      

      然后在每次迭代后增加$id。

      您不需要会话变量将值传递到
      edit.php
      文件

      希望有一个唯一的键来识别数据库中的记录。我假设它是
      recordid
      。将此值作为查询字符串传递给
      edit.php

      tag_show.php

      $output = '';
      while($row = mysqli_fetch_assoc($get_detail)){
          $output '<tr>
          <td>'.$row['tag_title'].'</td>
          <td>'.$row['description'].'</td>
          <td>'.$row['show_in_welcome'].'</td>
          <td>'.$row['status'].'</td>
          <td><a href="edit.php?editid='.$row['recordid'].'">edit</a></td>
          </tr>';
      }
      

      这里的会话毫无意义。每次循环运行之前替换的值时,都会得到最后一个值。您可以使用查询字符串的概念来代替。
      $output = '';
      while($row = mysqli_fetch_assoc($get_detail)){
          $output '<tr>
          <td>'.$row['tag_title'].'</td>
          <td>'.$row['description'].'</td>
          <td>'.$row['show_in_welcome'].'</td>
          <td>'.$row['status'].'</td>
          <td><a href="edit.php?editid='.$row['recordid'].'">edit</a></td>
          </tr>';
      }
      
      <?php
      
      if($_GET['editid']!=''){
          //get record corresponding to the 'editid' from database
          //I assume the record is for and store in a variable $editdata
      ?>
      <form action="tag_show.php" method="post">
      <br><br><br>
      Tag Title<br><input name="tag_title" type="text" value="<?=$editdata['tag_title']?>"><br><br>
      Description <br><input name="description" type="text" value="<?=$editdata['description']?>"><br><br>
      Show in welcome<br><input name="show_in_welcome" type="text" value="<?=$editdata['show_in_welcome']?>"><br><br>
      Status<br><input name="status" type="text" value="<?=$editdata['status']?>"> <br><br>
      <input name="submit1" type="submit"  value="Update">
      </form>
      <?php
      }
      ?>