【问题标题】:unnecessary notice from php [duplicate]来自php的不必要通知[重复]
【发布时间】:2013-08-22 11:22:01
【问题描述】:

最近我只是用通用脚本来配置我的脚本来输入数据 在我尝试提交数据时,数据提交成功。但是有一些通知让我感到困扰,他们说注意:未定义索引:在第 7 行输入 D:\xampp\htdocs\project\submit.php

这条线是

<?php
include 'includes/config.php';


if($_SERVER["REQUEST_METHOD"] == "POST")
{
$type=addslashes($_POST['type']); // this is line 7
$nama_barang=addslashes($_POST['nama_barang']);
$kategori=addslashes($_POST['kategori']);
$deskripsi=addslashes($_POST['deskripsi']);

我正在使用 xampp v.3.2.1 ,通知可能来自 xampp 吗? 谢谢你们,我很高兴你的回答:))

【问题讨论】:

  • 你需要学习如何READ and debug错误信息。解决问题所需的一切都在错误消息中。 $_POST['type'] 未定义
  • 检查这些可能性 1.可能是字段名称type未定义name = 'type' 2.可能使用表单方法GET
  • 旁注:仅仅使用addslashes() 通常表明代码中确实有问题...

标签: php html xampp


【解决方案1】:

类型(和其他 $_POST 成员)可能并不总是被设置,因此您应该尝试编写代码来检测它。

例如:

$type = (isset($_POST['type'])) ? addslashes($_POST['type']) : false;

【讨论】:

    【解决方案2】:

    通知中提到您的$_POST 数组没有索引type。 所以你应该在尝试访问它之前检查它:

    $type = ""; //you could set a default here
    if(array_key_exists("type", $_POST))
        $type = addslashes($_POST['type']);
    

    【讨论】:

    • isset($_POST['title']) 而不是 array_key_exists() 可能会更好。不仅因为性能非常轻微,而且因为即使 $_POST['type'] 设置为 null,array_key_exists() 也会返回 true。总的来说,最好维护你的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多