【问题标题】:PHP MySQL - Undefined Index in Edit DataPHP MySQL - 编辑数据中的未定义索引
【发布时间】:2014-03-30 23:17:04
【问题描述】:

我制作了文件details.php,它显示了来自MySQL的数据列表,我为此制作了edit.php。 当我点击edit.php 时,它会自动转到edit.php?id=detailsid(它有效),但是当我点击提交编辑数据时,它没有任何变化并且出现错误。

这是我的Edit.php

    <form method="post" action="editdata.php">
<?php 
          include 'config.php';
          $id=$_GET['id'];
          $sqlTampil="select * from data_korban Where kasus_id=$id"; 
          $qryTampil=mysql_query($sqlTampil); 
          $dataTampil=mysql_fetch_array($qryTampil); 
          $data=mysql_fetch_array($qryTampil);
         ?>

这是我的一些 HTML 表单

    <input type="DATE" name="tanggal" size="20" value="<?php echo $dataTampil['tanggal']; ?>">
    <input type="text" name="namakorban" size="40" value="<?php echo $dataTampil['namakorban']; ?>">
    <input type="text" name="umurkorban" size="5" value="<?php echo $dataTampil['umurkorban']; ?>">

<input type="submit" name="submit" value="SUBMIT">

当我点击提交时,它会自动调用editdata.php,但我的 MySQL 行没有变化..

这是我的editdata.php

<?php 
include "config.php";

$id=$_GET['id'];
$tanggal = $_POST['tanggal'];
$namakorban = $_POST['namakorban'];
$umurkorban = $_POST['umurkorban'];

$update = "UPDATE data_korban SET tanggal='$tanggal', namakorban='$namakorban', umurkorban='$umurkorban' WHERE kasus_id = '$id'";

$hasil = mysql_query($update);

if ($hasil) 
    echo "<center>Update Success<center>"; 
else
    echo "<center><h3><a href=pencarian.php>Back Tampil Data</a></h3></center>"; 
?>

我的 Error_Log 中有一些东西

PHP Notice:  Undefined index:  id in /home/lombokdi/public_html/dbanak/editdata.php on line 4 ------ it's $id=$_GET['id'];

我已更改为$_POST['id']; 仍然是错误。

【问题讨论】:

  • 您正在使用 $_GET 数组,而
    method="post"。使用 $_POST。如果 $_POST 给出错误 - 检查您是否有一个带有 name="id"
    的输入
  • 因为您没有将 id 传递给您的 editdata.php。既没有 GET 也没有 POST

标签: php mysql indexing undefined


【解决方案1】:

您的表单似乎不包含 id 命名字段。

您正在尝试从请求中读取不存在的字段。 请求通过post 方法发送数据,但您尝试使用get 方法读取id 参数。由于未找到 id 的值,因此在 where 子句中使用了 null 并且不会影响更新的行。

$id=$_GET['id'];
$tanggal = $_POST['tanggal'];
$namakorban = $_POST['namakorban'];
$umurkorban = $_POST['umurkorban'];

ID 的值为$id=$_GET['id']; ... id 来自detail.php?id=24(示例)转到edit.php?id=24 .. 如果我错了,如何设置ID??

edit.php 中,将id 命名元素添加到表单中,并从请求中读取值。在提交表单时,id 会转到 editdata.php 页面。

<input type="DATE" name="tanggal" size="20" value="<?php echo $dataTampil['tanggal']; ?>">
<input type="hidden" name="id" value="<?php echo $id['id']; ?>">
<input type="text" name="namakorban" size="40" value="<?php echo $dataTampil['namakorban']; ?>">
<input type="text" name="umurkorban" size="5" value="<?php echo $dataTampil['umurkorban']; ?>">

editdata.php:
变化:

$id=$_GET['id'];

收件人:

$id=$_POST['id'];

【讨论】:

  • 对不起,我是php新手,ID的值为$id=$_GET['id']; CMIIW 和 id 来自 detail.php?id=24 (example) 去edit.php?id=24 ..如果我错了,如何设置ID ??
  • 非常感谢.. 它的工作.. 我添加了
  • text 更改为hidden,除非你真的想编辑它。或者给元素添加readonly属性。
【解决方案2】:

这是因为你切换到editdata.php时没有提交的“id”

添加:&lt;input type="hidden" name="id" value="&lt;?php $_GET['id']; ?&gt;"&gt; 到 Edit.php 中的表单

并替换 $id=$_GET['id'];$id= $_POST['id']; 在 editdata.php 中。

【讨论】:

    【解决方案3】:

    这样试试

    $id=$_GET['id'];
    $sql=mysqli_query($con,"Update client set nama='$nama',instansi='$instansi',telepon='$telepon',email='$email' where id = '".$_GET['id']."'");
        if($sql)
        {
            $msg="Your Client updated Successfully";
            header('location:manage-client.php');
        }
    

    在正文后添加此查询

    $sql=mysqli_query($con,"select * from client where id = '".$_GET['id']."'");
                                        while($data=mysqli_fetch_array($sql))
    

    【讨论】:

      【解决方案4】:

      您正在使用 $_GET 来获取 id。最好是var_dump($_REQUEST);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-26
        • 2013-11-03
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        相关资源
        最近更新 更多