0

Im having problems wrapping my head around a code that I mean should work fine.

Here is the code:

        foreach( $_POST['wageline'] AS $line ){
            print_r( $line );
            if( !$MySQL->Query(" 
                INSERT INTO
                    wages_lines
                SET
                    'batch_id' = '" . $this->batch_id . "',
                    'name' = '" . $line['name'] . "',
                    'identity_number' = " . $line['identity_number'] . "',
                    'account' = '" . $line['account'] . "',
                    'phone' = '" . $line['phone'] . "',
                    'ordinary_hours' = '" . $line['ordinary_hours'] . "',
                    'fifty_percent' = '" . $line['fifty_percent'] . "',
                    'hundred_percent' = '" . $line['hundred_percent'] . "',
                    'mileage' = '" . $line['mileage'] . "',
                    'remuneration' = '" . $line['remuneration'] . "',
                    'pall_dag_2' = '" . $line['pall_dag_2'] . "',
                    'pall_dag_3' = '" . $line['pall_dag_3'] . "',
                    'pall_natt_2' = '" . $line['pall_natt_2'] . "',
                    'pall_natt_3' = '" . $line['pall_natt_3'] . "',
                    'ub_1' = '" . $line['ub_1'] . "',
                    'ub_2' = '" . $line['ub_2'] . "',
                    'ub_3' = '" . $line['ub_3'] . "',
                    'ub_4' = '" . $line['ub_4'] . "',
                    'ub_5' = '" . $line['ub_5'] . "',
                    'employee_wage' = '" . $line['employee_wage'] . "',
                    'brutto_total' = '" . $line['brutto_total'] . "',
                    'mileage_total' = '" . $line['mileage_total'] . "',
                    'remuneration_total' = '" . $line['remunerations_total'] . "'
            ") )

The result of my print_r is as following:

Array
(
['name'] => Alexander Fagerstrand
['identity_number'] => 
['account'] => 
['phone'] => 0
['ordinary_hours'] => 32.5
['fifty_percent'] => 0
['hundred_percent'] => 0
['mileage'] => 458
['remuneration'] => 628
['pall_dag_2'] => 0
['pall_dag_3'] => 0
['pall_natt_2'] => 0
['pall_natt_3'] => 0
['ub_1'] => 0
['ub_2'] => 0
['ub_3'] => 0
['ub_4'] => 0
['ub_5'] => 0
['employee_wage'] => 177
['brutto_total'] => 5752.50
['mileage_total'] => 1854.9
['remunerations_total'] => 628
)

And still, Im getting notices as these:


Notice: Undefined index: name in /var/www/vhosts/ontimebemanning.no/portal.ontimebemanning.no/modules/core.wages/classes/wage.class.php on line 573

Notice: Undefined index: identity_number in /var/www/vhosts/ontimebemanning.no/portal.ontimebemanning.no/modules/core.wages/classes/wage.class.php on line 574

Obviously, they are defined, so I have no clue why my script is throwing notices at my face. Anyone got a clue?

Ole Haugset
  • 3,709
  • 3
  • 23
  • 44
  • ['identity_number'] => ['account'] => they seem undefined to me – Marco Mura Nov 17 '14 at 14:01
  • @MarcoMura values seems undefined but keys are still present – pomeh Nov 17 '14 at 14:02
  • @MarcoMura they are defined but no value assigned – AlexL Nov 17 '14 at 14:02
  • The values are undefined, yes, but the index is not. Its also throwing the undefinex index error on indexes containing values – Ole Haugset Nov 17 '14 at 14:02
  • When I use print_r, I do not see quotes. I see something like [key] => value. Because you are seeing quotes, it tells me that you have quotes embedded into the key values - or you typed this by hand and added the quotes. If the quotes are in the key values, as shown, the keys without the quotes are not defined. – kainaw Nov 17 '14 at 14:05
  • pls post a var_dump($_POST['wageline']) – donald123 Nov 17 '14 at 14:05
  • In MySQL, quotes are solely for strings and not for identifiers. – Gumbo Nov 17 '14 at 14:09
  • you have to remove quote in the end of `'identity_number' = " . $line['identity_number'] . "',` or add quote after `=` if `$line['identity_number'] ` is string – dyachenko Nov 17 '14 at 14:18
  • You are vulnerable to [sql injection attacks](http://bobby-tables.com). – Marc B Nov 17 '14 at 14:20
  • @j.d. You've one quote too many `'identity_number' = " . $line['identity_number'] . "',` --- `'identity_number' = " . $line['identity_number'] . ",` – Funk Forty Niner Nov 17 '14 at 14:23

2 Answers2

4

If that's the output of print_r() the data that was sent in the POST request has quotes around the key names; to access the data now you would have to do this:

echo $lines["'name'"];

Alternatively, and more preferably, find out why those extra quotes are transferred and remove them.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

You have an error in your code. You are using => instead of = in your query code.

AlexL
  • 1,699
  • 12
  • 20
  • However, the problem of undefined indexes still consist – Ole Haugset Nov 17 '14 at 14:05
  • 2
    If something is wrong in the question that does not directly address the actual question, or is unrelated to the question, do not post a solution for that as an answer, but instead post a comment. Answers to a question should answer **that** question, not random things you found lying around that might be possibly remotely useful. – Sumurai8 Nov 17 '14 at 14:09