[英]Array to string conversion PHP SQL
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);
}
} }
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:您正在完全覆盖 $arr 变量,而您需要为当前循环使用临时变量:
$tempArr = $arr[$key];
and reference that in your INSERT query instead.并在您的 INSERT 查询中引用它。
Also, you should be using prepared statements to prevent SQL injection: How can I prevent SQL injection in PHP?此外,您应该使用准备好的语句来防止 SQL 注入: 如何在 PHP 中防止 SQL 注入?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.