PHP错误未定义的索引

时间:2016-08-09 04:15:17

标签: php

错误:

Image of the error

代码:

<?php
  if($_POST['submit']) {
    $ename = $_REQUEST['ename'];
    $civstatus = $_REQUEST['civstatus'];
    $deptno = $_REQUEST['deptno'];
    $hiredate = $_REQUEST['hiredate'];
    $sal = $_REQUEST['sal'];
    $comm = $_REQUEST['comm'];

    include("conn.php");
    $sql = "insert into emp
            (ename,civstatus,
             deptno,hiredate,
             sal,comm) values(
             '$ename','$civstatus',
             '$deptno','$hiredate',
             '$sal','$comm')";
    $res = $conn->query($sql);  
    $conn->close();
  }
?>

2 个答案:

答案 0 :(得分:0)

首先在您的表单中请检查您的提交按钮有name =“submit”,并在您的PHP代码中请使用isset。试试这个 -

<?php
  if(isset($_POST['submit'])) {
    $ename = $_REQUEST['ename'];
    $civstatus = $_REQUEST['civstatus'];
    $deptno = $_REQUEST['deptno'];
    $hiredate = $_REQUEST['hiredate'];
    $sal = $_REQUEST['sal'];
    $comm = $_REQUEST['comm'];

    include("conn.php");
    $sql = "insert into emp
            (ename,civstatus,
             deptno,hiredate,
             sal,comm) values(
             '$ename','$civstatus',
             '$deptno','$hiredate',
             '$sal','$comm')";
    $res = $conn->query($sql);  
    $conn->close();
  }
?>

答案 1 :(得分:0)

您需要检查所有表单字段的输入元素名称与您在php文件中使用的名称相同。

您需要通过检查值 -

等可用性来改进代码
<?php
  if(isset($_POST['submit']) {
    $ename = isset($_REQUEST['ename']) ? $_REQUEST['ename'] : '';
    $civstatus = isset($_REQUEST['civstatus']) ? $_REQUEST['civstatus'] : '';
    $deptno = isset($_REQUEST['deptno']) ? $_REQUEST['deptno'] : 0;
    $hiredate = isset($_REQUEST['hiredate']) ? $_REQUEST['hiredate'] : '';
    $sal = isset($_REQUEST['sal']) ? $_REQUEST['sal'] : 0;
    $comm = isset($_REQUEST['comm']) ? $_REQUEST['comm'] : ''; 

    include("conn.php");

    $sql = "insert into emp
            (ename,civstatus,
             deptno,hiredate,
             sal,comm) values(
             $ename,$civstatus,
             $deptno,$hiredate,
             $sal,$comm)";

    $res = $conn->query($sql);  
    $conn->close();
  }
?>

您还需要检查conn.php文件中是否有任何未定义的索引。