当我按编辑时,它不会自动填充表格中的输入文本

时间:2018-12-21 13:33:53

标签: php mysqli get

在index.php上有链接编辑,当我按edit时,应将其重定向到edit.php并根据我按edit的行自动填写表格,但不会根据表格进行填写

index.php

 <?php 
 $tampil="SELECT * FROM obat";
 $result=mysqli_query($conn,$tampil);
 if(mysqli_num_rows($result)){
    while ($row=mysqli_fetch_assoc($result)) {
    ?>
    <tr>
       <td><?=$row['id_obat']?></td>
       <td><?=$row['namaObat']?></td>
       <td><?=$row['jenisObat']?></td>
       <td><?=$row['stok']?></td>
       <td><a class="btn btn-success" href="edit.php">Edit</a>
           <a class="btn btn-danger" href="">Delete</a>
           </td>
    </tr>
    <?php
    }
     }
?>

edit.php

<?php 
    include('config.php');

        $id_obat = 'id_obat';
        $namaObat = 'namaObat';
        $jenisObat = 'jenisObat';
        $stok = 'stok';

        if (isset($_GET['id_obat'])) {
            $sql="SELECT * FROM obat WHERE id_obat=$id_obat";
            $result=mysqli_query($conn,$sql);
            $row=mysqli_fetch_assoc($result);
        }
        ?>
        <form method="POST" action="edit.php">
          <div>
            <label>Nama Obat</label>
            <input type="text" class="form-control" name="namaObat" value="<?=$namaObat?>">
        </form>

1 个答案:

答案 0 :(得分:0)

href="edit.php"之后没有get参数。即href="edit.php?id_obat=<?php echo $row['id_obat'];?>"

在编辑页面中,您没有将GET参数分配给$id_obat,而只是分配了字符串id_obat。

您应该首先查看SQL Injection并转义任何GET参数,甚至最好使用Prepared Statements

请以以下示例为例,并查看我发布的两个保护数据库的链接。更改是...检查GET参数,然后将其分配给变量。

include('config.php');       
    $namaObat = 'namaObat';
    $jenisObat = 'jenisObat';
    $stok = 'stok';

    if (isset($_GET['id_obat'])) {
        $id_obat = mysql_real_escape_string($_GET['id_obat']);

        $sql="SELECT * FROM obat WHERE id_obat=$id_obat";
        $result=mysqli_query($conn,$sql);
        $row=mysqli_fetch_assoc($result);
    }
    ?>
    <form method="POST" action="edit.php">
      <div>
        <label>Nama Obat</label>
        <input type="text" class="form-control" name="namaObat" value="<?=$namaObat?>">
    </form>