Notice: Undefined index: ID in C:\xampp\htdocs\tugas\phpdb\ubahsimpan.php on line 10
im tired ,,,
how i can fix that?

My ubahdata.php

 <html> <body> <?php
$koneksi=mysqli_connect("localhost","root","") or die ("Koneksi Gagal".mysqli_error());
if($koneksi){
    mysqli_select_db($koneksi,"buku") or die ("Database gagal dibuka".mysqli_error());
    $ID=$_GET['ID'];
    //Mejalankan Querry
    $sql="select * from anggota where id_anggota='$ID'";
    $qry=mysqli_query($koneksi, $sql) or die ("Querry gagal".mysqli_error());
    $data=mysqli_fetch_array($qry);
    $ID=$data['id_anggota'];
    $nama=$data['nm_anggota'];
    $alamat=$data['alamat'];
    if($data['kelamin']=="P"){
    $cekp="checked";
    $cekw="";}
    else {
    $cekp="";
    $cekw="checked";}
    }
?> <form action="ubahsimpan.php" method="POST" name="form1" target="self"> <table width="350" border="1"> <tr> <td colspan="2">Ubah Data Anggota </td> </tr> <tr> <td width="103">ID Anggota</td> <td width="222"> <input name="ID" type="text" size="30" maxlength="35" value="<?php echo $data['id_anggota'];?>" disabled /> </td> </tr> <tr> <td> Nama </td> <td> <input name="TxtNama" type="text"  size="30"  value="<?php echo $data['nm_anggota'];?>" maxlength="35"/> </td> </tr> <tr> <td>Alamat</td> <td> <input name="TxtAlamat" type="text"  size="30" value="<?php echo $data['alamat'];?>" maxlength="60" /> </td> </tr> <tr> <td>Jenis Kelamin</td> <td><label> <input name="RbKelamin" type="radio" value="P" checked>
                Pria
                <input name="RbKelamin" type="radio" value="W" checked>
                Wanita
</label></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="simpan" /> </td> </tr> </table> </form> </body> </html>

My ubahsimpan.php

<html> <body> <?php
    $koneksi=mysqli_connect("localhost","root","")
    or die("Gagal konek server".mysqli_error());

    if($koneksi){
    mysqli_select_db($koneksi,"buku")
    or die("Database gagal dibuka".mysqli_error());
    $ID=$_POST['ID'];
    $TxtNama=$_POST['TxtNama'];
    $TxtAlamat=$_POST['TxtAlamat'];
    $RbKelamin=$_POST['RbKelamin'];
$sql="update anggota set nm_anggota='$TxtNama',
alamat='$TxtAlamat',kelamin='$RbKelamin' where id_anggota='$ID'";
$qry=mysqli_query($koneksi, $sql)or die("Gagal Query ubah:".mysqli_error());
echo "Data berhasil diubah";
include "ubahtampil.php";
}?> </body> </html>

Dani AI

Generated

As noted, the immediate cause of the "undefined index" error is that the ID value never reaches the POST handler. In the posted form the visible ID input was disabled — disabled inputs are not submitted. Keep the visible ID readable for users but submit the real ID with a hidden field, and always check for the presence of the field on the server before using it.

A minimal pattern that fixes the submission (visible readonly field + hidden field) looks like this:

<input type="text" name="ID_display" value="<?php echo htmlspecialchars($data['id_anggota']); ?>" readonly>
<input type="hidden" name="ID" value="<?php echo htmlspecialchars($data['id_anggota']); ?>">

On the server side, validate that the POST variables exist and use a prepared statement to update the database, for example:

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ID'])) {
    $id = $_POST['ID'];
    $name = $_POST['TxtNama'];
    $alamat = $_POST['TxtAlamat'];
    $kelamin = $_POST['RbKelamin'];

    $stmt = $mysqli->prepare("UPDATE anggota SET nm_anggota=?, alamat=?, kelamin=? WHERE id_anggota=?");
    $stmt->bind_param("ssss", $name, $alamat, $kelamin, $id);
    $stmt->execute();
}

Troubleshooting and hardening notes: 1) Inspect the actual form payload in the browser DevTools Network tab or run var_dump($_POST) to confirm what is submitted. 2) Fix the radio inputs — do not hard-code checked on both; set checked based on $data['kelamin']. 3) Escape values used inside HTML attributes with htmlspecialchars() to prevent markup issues. 4) Prefer prepared statements (or at least proper escaping) and graceful error handling rather than exposing raw database errors. These changes address the immediate undefined-index error and make the update flow more robust.

You use $_GET['ID'] while your form uses method="POST"

commented: Will still err as OP uses $ID in string without dots - "WHERE id_anggota = '".$ID."'" with proper apostrophes +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.