为什么来自URL的值不会传递给粘性形式

  • 本文关键字:URL php
  • 更新时间 :
  • 英文 :


我已经创建了PHP粘性表单,因此单击"提交"按钮时数据不会消失。URL链接被用于将值传递给表单,以便可以编辑它们。但是,来自URL的值并未传递到表单字段中。为什么来自URL的值未传递到表单字段中?非常感谢您的参与。

这是代码:

index.php

<?php
  require_once('authorize.php');
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
  require_once('appvars.php');
  require_once('connectvars.php');

  $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  $data = mysqli_query($conn, $query);
  echo '<table>';
  echo '<tr><th>Name</th><th>Caption</th><th>Action</th></tr>';
  while ($row = mysqli_fetch_array($data)) { 

      //link
    echo '<td><a href="link.php?id=' . $row['id'] . '&amp;image=' . $row['image1'] . '&amp;name=' . $row['name'] . 
    '&amp;caption=' . $row['caption'] .
      '&amp;video=' . $row['video'] . '">Edit </a>'; 

    echo '</td></tr>';
  }
  echo '</table>';   
  echo "<br><br>";
  mysqli_close($conn);
?>
</body> 
</html> 

sticky_form.php

<!DOCTYPE html>
<html>
<head>
  <title>Edit Conent</title>
</head>
<body>
  <h3>Edit Conent</h3>
<?php
  require_once('appvars.php');
  require_once('connectvars.php');
  $vid="";
  $vname="";
  $vcaption="";
  $vvideo="";
  $id =""; 

  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  if(isset($_POST["button_edit"])){
     $id = $_POST["id"];
    $name = $_POST['name']; 
    $caption = $_POST['caption']; 
    $video = $_POST['video'];

    $qry = mysqli_query($dbc,"Update table1 Set name='$name', caption='$caption', video='$video' Where id='$id'");

else if(isset($_GET["id"])){
    $qry = mysqli_query($dbc,"Select * From table1 Where id='".$_GET["id"]."'");
    while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
        $vid=$row["id"];
        $vname=$row["name"];
        $vcaption=$row["caption"];
        $vvideo=$row["video"];

    }
}

?>

<body>
<form action='' method="post" enctype="multipart/form-data" >
    <table>
    <tr>
            <td>ID</td>
            <td><input type="text"  name="id" value="<?php echo $vid;?>"></td></tr>
        <tr>
            <td>Name</td>
            <td><input type="text" class="bigger_textbox" name="name" value="<?php if (isset($_POST['name'])) {echo htmlentities($_POST['name']);}?>"></td></tr>
        <tr><td>Caption</td>
        <td><input type="text" class="bigger_textbox" name="caption"  value="<?php if (isset($_POST['caption'])) {echo htmlentities($_POST['caption']);}?>"></td></tr>
        <tr><td>Video</td>
        <td><input type="text" class="bigger_textbox" name="video"  value="<?php if (isset($_POST['video'])) {echo htmlentities($_POST['video']);}?>"></td></tr>
        <tr><td colspan="2">
        <input type="submit" name="button_edit" value="Edit Content"></td></tr> </table>
</form>
<table border=1>
    <tr><th>Name</th><th>Caption</th>
    <th>Video</th>  <th>Action</th></tr>
    <?php
     if (isset($_GET["id"])) {
    $qry =mysqli_query($dbc, "Select * From table1 Where id='".$_GET["id"]."'");
    while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)) {
        echo '<tr><td>'.$row["name"].'</td>';
        echo '<td>'.$row["caption"].'</td>';
        echo '<td>'.$row["video"].'</td>'; 
        echo '<td><a href="?id='.$row["id"].'&edit='.$row["id"]. '&video='.$row["video"].'">Edit</a> </td></tr>';
        }

    }

    ?>
</table>
</body>
</html>

显然您已经有stick_form.php中需要的值:

else if(isset($_GET["id"])){
$qry = mysqli_query($dbc,"Select * From table1 Where id='".$_GET["id"]."'");
while($row=mysqli_fetch_array($qry,MYSQLI_ASSOC)){
    $vid=$row["id"];
    $vname=$row["name"];
    $vcaption=$row["caption"];
    $vvideo=$row["video"];

}

尝试替换stick_form.php的代码的这一部分:

   <td><input type="text" class="bigger_textbox" name="name" value="<?php if (isset($_POST['name'])) {echo htmlentities($_POST['name']);}?>"></td></tr>
   <tr><td>Caption</td>
   <td><input type="text" class="bigger_textbox" name="caption"  value="<?php if (isset($_POST['caption'])) {echo htmlentities($_POST['caption']);}?>"></td></tr>
   <tr><td>Video</td>
   <td><input type="text" class="bigger_textbox" name="video"  value="<?php if (isset($_POST['video'])) {echo htmlentities($_POST['video']);}?>" </td></tr>

with:

<td><input type="text" class="bigger_textbox" name="name" value="<?php echo $vname; ?>"></td></tr>
<tr><td>Caption</td>
<td><input type="text" class="bigger_textbox" name="caption"  value="<?php echo $vcaption; ?>"></td></tr>
<tr><td>Video</td>
<td><input type="text" class="bigger_textbox" name="video"  value="<?php echo $vvideo; ?>"></td></tr>

更新

您评论时,单击"编辑"按钮后,您的表单字段变为空。那是因为您没有在代码的这一部分中设置正确的变量:

if(isset($_POST["button_edit"])){
     $id = $_POST["id"];
    $name = $_POST['name']; 
    $caption = $_POST['caption']; 
    $video = $_POST['video'];

    $qry = mysqli_query($dbc,"Update table1 Set name='$name', caption='$caption', video='$video' Where id='$id'");

将其更改为:

if(isset($_POST["button_edit"])){
     $vid = $_POST["id"];
    $vname = $_POST['name']; 
    $vcaption = $_POST['caption']; 
    $vvideo = $_POST['video'];

    $qry = mysqli_query($dbc,"Update table1 Set name='$vname', caption='$vcaption', video='$vvideo' Where id='$vid'");

希望它有帮助。

相关内容

  • 没有找到相关文章

最新更新