The given code is a demonstration of crud operation in PHP.
the basic HTML form has three field NAME
, EMAIl
, DEPT
, and a submit button.
the basic operations are working fine add delete and form submission but when I want to update the data it gave
Undefined index: id in C:\xampp\htdocs\test\edit.php on line 13.
but when I changed the query in my edit.php filed id to name it will work fine but it is not working when a change the name to id in my query in edit.php file the table name is crud
. and id
is the primary key in the table. the id is auto incremented. I don't understand why it is working with name and not to work with id filed.
<html>
<head>
<title>CRUD</title>
</head>
<style type="text/css">
form{
text-align: center;
margin-top:50px;
font-family:serif;
color: grey;
}
input{
margin:10px;
}
</style>
<body>
<form action="submit.php" method="POST">
ID:<input type="text" name="id" style="width:200px"; disabled required placeholder="Autogenrated">
<br>
Name:<input type="text" name="name" style="width:200px";required><br>
email:<input type="email" name="email" style="width:200px";required><br>
Dept:<input type="text" name="dept" style="width:200px"; required><br>
<button type="submit" name="submit" style="width:80px";>Submit</button>
</form>
</div>
</body>
</html>
<?php
include_once('db.php');
if (isset($_GET['edit'])){
$id=$_GET['edit'];
$sql=$conn->query("select * from crud where id=$id");
while($row=$sql->fetch_array()){
$name=$row['name'];
$email=$row['email'];
$dept=$row['dept'];
}
}
if (isset($_POST['update'])){
$id=$_POST['id'];
echo $id ;
}
?>
<form action="" method="POST">
ID:<input type="text" name="id" value="<?php echo $id ?>" style="width:200px"; disabled required placeholder="Autogenrated" >
<br>
Name:<input type="text" name="name" value="<?php echo $name ?>" style="width:200px";required><br>
email:<input type="email" name="email" value="<?php echo $email ?>" style="width:200px";required><br>
Dept:<input type="text" name="dept" value="<?php echo $dept ?>" style="width:200px"; required><br>
<button type="submit" name="update" style="width:80px";>Update</button>
</form>
<?php
include_once('db.php');
if (isset($_GET['del'])) {
$id=$_GET['del'];
$sql="delete from crud where id='$id'";
mysqli_query($conn,$sql);
}
header('Location:display.php');
?>
<?php
include_once('db.php');
$sql="select * from crud";
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
while ($row=mysqli_fetch_assoc($result)) {
echo "--Name:".$row["name"]."--Email:".$row["email"]." "."--dept:".$row["dept"]."";
echo "<br>";
echo '<td><a href="edit.php?edit=' . $row['id'] . '">Edit</a></td>';
echo '<td><a href="delete.php?del=' . $row['id'] . '">Delete</a></td>';
echo "<br>";
echo '<td><a href="index.php?">add</a></td>';echo "<br>";
}
}
?>
<?php
$hostname="localhost";
$user="root";
$pass="";
$dbname="test";
$conn=mysqli_connect($hostname,$user,$pass,$dbname);
?>
change this
if (isset($_POST['update'])){
$id=$_POST['id'];
echo $id ;
}
to
if (isset($_POST['update'],$_POST['id'])){
$id=$_POST['id'];
echo $id ;
}