PHP 注意:未定义的偏移量 [英] PHP Notice: Undefined offset

查看:41
本文介绍了PHP 注意:未定义的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用复选框更新多行.我的脚本有效,但在 ini_set display_errors 后显示错误消息

代码如下:

$count=mysql_num_rows($result);if (isset($_POST['submit'])) {$checked = $_POST['checkbox'];for ($i = 0; $i < $count; $i++) {$result = mysql_query("UPDATE trace SET status='Delivered' WHERE id='$checked[$i]'") or die(mysql_error());}}

显示的是Notification: Undefined offset row number

怎么了?

解决方案

错误

注意:未定义偏移

本质上是说您试图引用不存在的数组的值.

检查您的代码,有两种可能的情况会发生这种情况,第一个 $_POST['checkbox'] 和第二个 $checked[$i].

你可以通过这样的方式解决这个错误

if (isset ($_POST['submit'])){$checked = isset($_POST['checkbox']) ?$_POST['checkbox'] : null;如果 (is_array($checked)){foreach ($checked as $check){$result = mysql_query("UPDATE trace SET status='Delivered' WHERE id='$check'") or die(mysql_error());}}}

注意上面的代码不应该在生产中使用,因为它不安全.

另请注意,mysql_ 函数已被弃用.使用 PDO 或 mysqli 进行数据库查询.

I want update multiple rows using checkbox. my script works, but showing error messages after ini_set display_errors

Here is code:

$count=mysql_num_rows($result);
if (isset($_POST['submit'])) {
    $checked = $_POST['checkbox'];
    for ($i = 0; $i < $count; $i++) {
        $result = mysql_query("UPDATE trace SET status='Delivered' WHERE id='$checked[$i]'") or die(mysql_error());
    }
}

It's showing Notice: Undefined offset row number

What is wrong?

解决方案

The error

Notice: Undefined offset

is in essence saying that you have attempted to reference a value of an array that does not exist.

Reviewing your code, there are two possible instances where this can happen, first $_POST['checkbox'] and second $checked[$i].

You can resolve this error by something like this

if (isset ($_POST['submit']))
{
   $checked = isset($_POST['checkbox']) ? $_POST['checkbox'] : null;
   if (is_array($checked))
   {
     foreach ($checked as $check)
     {
        $result = mysql_query("UPDATE trace SET status='Delivered' WHERE id='$check'") or die(mysql_error());
     }
   }
}

Note the above code should not be used in production as it is not secure.

Note also that mysql_ functions are deprecated. Use PDO or mysqli for database queries.

这篇关于PHP 注意:未定义的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆