0

我在 PHP 中运行这个 SQL 查询:

$sql2="INSERT into dd_submissions (customer_seq, dd_reference, sortcode, account_number, account_name, amount, bacs_code, invoice_no, title, initial, forename, surname, salutation_1, salutation_2, address_1, address_2, area, town, postscode, phone, mobile, email, notes) values ('".$customer["sequence"]."', '$data[0]', '$data[1]', '$data[2]', '$data[3]', '$data[4]', '$data[5]', '$data[6]', '$data[7]', '$data[8]', '$data[9]', '$data[10]', '$data[11]', '$data[12]', '$data[13]', '$data[14]', '$data[15]', '$data[16]', '$data[17]', '$data[18]', '$data[19]', '$data[20]', '$data[21]', '$data[22]')";
            echo $sql2.'<br><br>';
            $rs2=mysql_query($sql2,$conn) or die(mysql_error());

但我在第 30 行收到此错误($sql2="INSERT into .....)

Notice: Undefined offset: 22 in /home/integra/public_html/admin/billing/upload_direct_debit_form.php on line 30
INSERT into dd_submissions (customer_seq, dd_reference, sortcode, account_number, account_name, amount, bacs_code, invoice_no, title, initial, forename, surname, salutation_1, salutation_2, address_1, address_2, area, town, postscode, phone, mobile, email, notes) values ('53', 'VOIP/INTERNET', '555028', '60974222', 'DRAGON ENTERPRISE CENTRE', '941.58', '17', '5847619', 'Mr', 'D.', '', 'Belcher', 'Daniel', '', '28 Stephenson Road', '', '', 'Leigh-on-Sea', 'SS9 5LY', '01702 511222', '', 'daniel@dragonenterprisecentre.co.uk', '', '')
4

2 回答 2

1

这不是错误。您的 PHP 配置启用了通知,因此如果您尝试引用不存在索引的数组位置,您将收到警报。根据消息,数组中不存在$data[22]。您需要以不假定每个数组偏移量都已初始化的方式编写此代码块。调试的最佳选择是执行以下操作:

echo print_r($data);
于 2013-08-03T20:43:03.403 回答
0

为每个值转义 SQL 查询。

固定查询:

$sql2="INSERT INTO
    dd_submissions
        (customer_seq, dd_reference, sortcode, account_number, account_name, amount, bacs_code, invoice_no, title, initial, forename, surname, salutation_1, salutation_2, address_1, address_2, area, town, postscode, phone, mobile, email, notes)
    VALUES ('".$customer['sequence']."', '".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."', '".$data[5]."', '".$data[6]."', '".$data[7]."', '".$data[8]."', '".$data[9]."', '".$data[10]."', '".$data[11]."', '".$data[12]."', '".$data[13]."', '".$data[14]."', '".$data[15]."', '".$data[16]."', '".$data[17]."', '".$data[18]."', '".$data[19]."', '".$data[20]."', '".$data[21]."', '".$data[22]."')";

此外,通知不是致命的执行错误。为防止它们显示非致命错误,请将其放在页面的绝对顶部:

error_reporting(E_ALL ^ E_NOTICE);

于 2013-08-03T20:42:52.747 回答