0

i am running this SQL Query in PHP:

$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());

but im getting this error on line 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', '', '[email protected]', '', '')
8
  • 1
    "Undefined offset" refers to a non-existant array index. According to the error message, it's in upload_direct_debit_form.php, on line 30. Commented Aug 3, 2013 at 20:40
  • line 30 is that SQL Query ($sql2) Commented Aug 3, 2013 at 20:41
  • The $data[22] array index is undefined at the end of the $sql2 string. Commented Aug 3, 2013 at 20:42
  • var_dump your $data-array and see if there's an item with index 22. Commented Aug 3, 2013 at 20:42
  • @Temek got it thanks, i have removed the $data[22] from the SQL but now its only running one SQL in the while loop. if i remove the INSERT SQL Query it lists 7 rows when i echo a variable in the while loop but when the insert query is there it stops at one Commented Aug 3, 2013 at 20:46

2 Answers 2

1

It's not an error. Your PHP configuration has notices enabled, so you will be alerted if you are trying to reference an array location with a non-existent index. According to the message, $data[22] does not exist in the array. You need to write this code block in such a way that it doesn't assume each array offset is initialized. Your best bet for debugging is to do the following:

echo print_r($data);
Sign up to request clarification or add additional context in comments.

1 Comment

"It's not an error." ... "trying to reference an array location with a non-existent index". Yeah, it's normal.
0

Escape your SQL query for each value.

Fixed query:

$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]."')";

In addition, notices are not fatal execution errors. To prevent them from showing non-fatal errors, put this at the ABSOLUTE TOP of your page:

error_reporting(E_ALL ^ E_NOTICE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.