首页 > 解决方案 > 未定义索引:第 13 行 C:\xampp\htdocs\test\edit.php 中的 id

问题描述

给定的代码是 PHP 中 crud 操作的演示。

基本的 HTML 表单具有三个字段NAMEEMAIlDEPT和一个提交按钮。

基本操作工作正常添加删除和表单提交但是当我想更新它提供的数据时

未定义的索引:第 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);
         ?>

标签: phpmysql

解决方案


改变这个

if (isset($_POST['update'])){
            $id=$_POST['id'];
            echo $id ;  
        }

if (isset($_POST['update'],$_POST['id'])){
            $id=$_POST['id'];
            echo $id ;  
        }

推荐阅读