0

im using phpexcel to read my xls, i dont have problems with that, but when im trying to insert values in my database appear this on console "Array to string conversion".

code:

 $inputFileName = $_FILES['xls']['tmp_name'];
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
    // ------------------ COMISIONES ------------------ //
    $sheet = $objPHPExcel->getSheet(2);
    $highestRow = $sheet->getHighestRow();
    $highestColumn = $sheet->getHighestColumn();
    for ($row = 1; $row <= $highestRow; $row++) {
        $rowData[] = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);            
        foreach ($rowData[0] as $var) {
          $sql = "INSERT INTO bd_comisiones (registro,clave,nombre,rfc,comision,iva,subtotal,retencion,neto,prestamos,neto_a_recibir) VALUES ('" . implode(",", $var) . "')";
          $sql_insert = $conexion->prepare($sql);
          $sql_insert->execute();
        }
    }

array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Registro
                    [1] => CLAVE
                    [2] => NOMBRE
                    [3] => RFC
                    [4] => COMISION
                    [5] => IVA
                    [6] => SUBTOTAL
                    [7] => RETENCION
                    [8] => NETO
                    [9] => PRESTAMOS
                    [10] => NETO_A_RECIBIR
                )

        )    
2
  • Have you checked value of $highestColumn? Commented Jul 28, 2015 at 19:07
  • On which position/line do you get the error? And shouldn't you put the contents of $var in single quotes? I mean like VALUES ('" . implode("','", $var) . "')";? Commented Jul 28, 2015 at 19:12

4 Answers 4

1

Looks like your implode function needs a slight rework.

If you have array('a','b','c') and you want that to be a string with quotes around each value, you need to revise this line:

$sql = "INSERT INTO bd_comisiones
  (registro,clave,nombre,rfc,comision,iva,subtotal,retencion,neto,prestamos,neto_a_recibir)
  VALUES ('" . implode(",", $var) . "')";

to be like this:

$sql = "INSERT INTO bd_comisiones
  (registro,clave,nombre,rfc,comision,iva,subtotal,retencion,neto,prestamos,neto_a_recibir)
  VALUES ('" . implode('","', $var) . "')";

This will put "," between each value, instead of just a comma.

If you're still troubleshooting it, try to echo $sql; to see the fully-formed SQL statement to make sure it looks okay.

Sign up to request clarification or add additional context in comments.

1 Comment

awesome, but now there is no value on my bd
1

Which line is your error occuring on?

And beyond that, your query will be incorrect:

$sql = "INSERT ... VALUES ('" . implode(",", $var) . "')";
                                        ^^^

This would produce

INSERT ... VALUES ('Registro,CLAV,...,NETO_A_RECIBIR')

which is a SINGLE string value. You want

$sql = "INSERT ... VALUES ('" . implode("','", $var) . "')";
                                         ^-^

isntead, so each individual component gets its own '...' quote set, producing

INSERT ... VALUES ('Registro','CLAV',...,'NETO_A_RECIBIR')

1 Comment

line 72, at query line by the way
0

I've never used PHP Excel but I think that the problem is in your insert statement: when you implode your array there are only two quotes!

Simply change your line with this:

$sql = "INSERT INTO bd_comisiones (registro,clave,nombre,rfc,comision,iva,subtotal,retencion,neto,prestamos,neto_a_recibir) VALUES ('" . implode("','", $var) . "')";

2 Comments

yeah, totally works, but i don't have any values in my bd >.< just end the procces but do nothing
I can only suggest you to dump your variables (first of all check $sqland try to execute that code directly on your db).
0

I thing you should put single quotes in the implode function like

VALUES ('" . implode("','", $var) . "')";

to get string like

VALUES ('A', 'B', 'C');

otherwise is just

VALUES ('A, B, C');

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.