charlie
charlie

Reputation: 1384

PHP undefined offset when running SQL Query

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]', '', '')

Upvotes: 0

Views: 4103

Answers (2)

user2647338
user2647338

Reputation: 61

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

Upvotes: 1

1234567
1234567

Reputation: 956

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

Upvotes: 0

Related Questions