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

}

2
  • 2
    You will have to include the code which does the insert for us to help fully. Commented Nov 10, 2020 at 9:24
  • im just edit my question, thanks :) Commented Nov 10, 2020 at 9:29

1 Answer 1

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?

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

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.