php - 未定义索引:第 13 行 C:\xampp\htdocs\test\edit.php 中的 id
问题描述
给定的代码是 PHP 中 crud 操作的演示。
基本的 HTML 表单具有三个字段NAME
、EMAIl
、DEPT
和一个提交按钮。
基本操作工作正常添加删除和表单提交但是当我想更新它提供的数据时
未定义的索引:第 13 行 C:\xampp\htdocs\test\edit.php 中的 id。
但是当我将edit.php文件中的查询更改为名称时,它可以正常工作,但是当我在edit.php文件中的查询中将名称更改为id时,表名是crud
。并且id
是表中的主键。id 是自动递增的。我不明白为什么它使用 name 而不是使用 id 归档。
<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);
?>
解决方案
改变这个
if (isset($_POST['update'])){
$id=$_POST['id'];
echo $id ;
}
至
if (isset($_POST['update'],$_POST['id'])){
$id=$_POST['id'];
echo $id ;
}
推荐阅读
- c++ - std::index_sequence_for 如何
() 到底有没有用? - r - R根据特定列值乘以不相等的数据帧
- python - 为什么在具有混合元素类型的 Pandas DataFrame 中,当我更改元素时出现“ValueError”?
- reactjs - 如何一次调用一个 API 挂钩?
- typescript - TypeORM+nestjs:如何使用父泛型序列化+CRUD 子实体类,具有单个控制器、存储库、子类型验证?
- html - 如何在Angular 7中加载页面后触发引导模式弹出窗口
- promise - 等待进行 http 调用的网络工作者完成
- laravel - Laravel Telescope Schedule 在 localhost(mac) 上不起作用
- c# - .Net Core 3.1 OpenIdConnect 与 AWS Cognito
- kubernetes - 如何使用 kubernetes 执行器为气流工作 pod 全局设置 request_cpu?