运行SQL Query时PHP未定义的偏移量

时间:2013-08-03 20:36:19

标签: php mysql

我在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', '', '')

2 个答案:

答案 0 :(得分:1)

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

echo print_r($data);

答案 1 :(得分: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);