1

im trying to print_r my code array :

$arr =  Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )

so, how i can get this value to insert to database? because when i submit always error Array to string conversion

<?php
if (isset($_POST['cmdSubmitAspek'])) 
{
$nilaiBobot = $_POST['nilaiBobot']; 
$arr = $_POST['arr']; 

foreach($_POST['nilaiBobot'] as $key => $value ){

 $nilaiBobotTenp = $nilaiBobot[$key];
 $arr = $arr[$key];


$queryInsAspek = "insert into skalaPrioritas (idDivisi, idDepartment, arr, nilaiBobot, submitBy, inputDT)
              VALUES  ".$idDivisi.", ".$idDepartment.", '".$arr."','".$nilaiBobotTenp."',".$userID.", getdate()";
$resultInsAspek = sqlsrv_query($conn, $queryInsAspek);           
}

}

1 Answers1

1

I expect the issue is that when you do:

$arr = $arr[$key];

you are completely overwriting the $arr variable, instead you need to use a temp variable for the current loop:

$tempArr = $arr[$key];

and reference that in your INSERT query instead.

Also, you should be using prepared statements to prevent SQL injection: How can I prevent SQL injection in PHP?

Matt
  • 1,073
  • 1
  • 8
  • 14