使用动态索引INSERT简单数组到Php-Mysql

时间:2021-03-14 21:33:27

how y can store in my BD Mysql this array:

如何在我的BD Mysql中存储这个数组:

Array ( [3] => 23 [9] => 54 )

Structure of Bd Table is:

Bd表的结构是:

ID_PROD |  QUANT
----------------------
3          23     
9          54

I tried with this code, but without results, my problem are the INDEXEs of array

我尝试使用此代码,但没有结果,我的问题是数组的INDEXE

$arr1 = $_GET['sols']; 

$sql = array(); 
foreach( $arr1 as $row ) {
$sql[] =   '('.$row['???????'].','.$row['??????????'].')';
}
mysql_query('INSERT INTO sales (ID_PROD,QUANT) VALUES '.implode(',', $sql));

1 个解决方案

#1


0  

You need to actually populate your query with the data from the array.

您需要使用数组中的数据实际填充查询。

$arr1 = $_GET['sols']; 

$sql = array(); 
foreach( $arr1 as $key => $row ) {
$sql[] =   '(' . $key . ',' . $row.')';
}
mysql_query('INSERT INTO sales (ID_PROD,QUANT) VALUES '.implode(',', $sql));

Also, please rework your code to not use mysql_query. Use the better PDO or mysqli frameworks (or something similar).

另外,请重新编写代码以不使用mysql_query。使用更好的PDO或mysqli框架(或类似的东西)。

#1


0  

You need to actually populate your query with the data from the array.

您需要使用数组中的数据实际填充查询。

$arr1 = $_GET['sols']; 

$sql = array(); 
foreach( $arr1 as $key => $row ) {
$sql[] =   '(' . $key . ',' . $row.')';
}
mysql_query('INSERT INTO sales (ID_PROD,QUANT) VALUES '.implode(',', $sql));

Also, please rework your code to not use mysql_query. Use the better PDO or mysqli frameworks (or something similar).

另外,请重新编写代码以不使用mysql_query。使用更好的PDO或mysqli框架(或类似的东西)。