【问题标题】:Troubleshooting "Undefined offset" error对“未定义的偏移量”错误进行故障排除
【发布时间】:2012-01-21 04:40:39
【问题描述】:

显示以下错误:

注意:未定义的偏移量:C:\xampp\htdocs\evantechbd\secure\content\feature_link_process.php 第 28 行中的 1。

为什么会显示错误...?谁能告诉我...这是代码:

$row = explode("|", $_POST['coun_name']);
$coun_id = $row[1]; // cat_id
$coun_name = $row[0];

if(isset($coun)){
    $errors = array();      
    if(empty($coun))
    $errors[] = 'Country Name require<br>';             
}

【问题讨论】:

  • 另外,请查看bobby-tables.com
  • 第 28 行:$coun_id = $row[1]; // cat_id $coun_name = $row[0];
  • 你能做一个print_r($row) 并将输出添加到你的问题吗?

标签: php


【解决方案1】:
$row = explode("|", $_POST['coun_name']);
$coun_id = $row[1]; // cat_id
$coun_name = $row[0];

如果 $_POST['coun_name] 没有 |比 $row[1] 没有定义。

【讨论】:

  • 感谢您的回复@rauschen。抱歉,我不明白,我的代码有什么问题,我该怎么办?
  • 我认为当 $_POST['coun_name'] 为 emtpy 时会出现问题 .. 未选择任何内容.. 您稍后已经检查 coun = $_POST['coun_name']; 是否为 emtpy .. 检查后会爆炸。 .
【解决方案2】:

你可能想要做这样的事情,而不是:

$errors = array();

if( empty($_POST['coun_name']) )
{
  $errors[] = 'Country Name required.';
}
elseif( strpos($_POST['coun_name'], '|') === false )
{
  $errors[] = 'Country Name invalid.';
}
else
{
  list($coun_name, $coun_id) = explode("|", $_POST['coun_name'], 2);
}

第一个条件检查以确保用户提交了$_POST['coun_name'] 的值。第二个条件检查以确保该值包含“|”字符。

如果满足这两个条件,$coun_name$coun_id 将像往常一样由explode()'ing $_POST['coun_name'] 填充(为简洁起见,我已致电list())。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2019-08-19
    相关资源
    最近更新 更多