我有一个像这种类型的json对象
[{"productName":"ddsf","productPrice":234234,"quantity":23}][{"productName":"ddsf","productPrice":234234,"quantity":23}]Array
(
[0] => Array
(
[productName] => ddsf
[productPrice] => 234234
[quantity] => 23
)
)
如何一次插入此对象数组。我的数据库列是id,productname,productprice,数量,所以我想一次插入多行
数据库连接示例:
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="dbname";
$username="username";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
mysql_select_db($database) or die('Could not select database');
数据库中INSERT数组的示例:
$json = file_get_contents('php://input');
$obj = json_decode($json,true);
//Database Connection
require_once 'db.php';
/* insert data into DB */
foreach($obj as $item) {
mysql_query("INSERT INTO `database product`.`table name` (productName, productPrice, quantity)
VALUES ('".$item['productName']."', '".$item['productPrice']."', '".$item['quantity']."')");
}
//database connection close
mysql_close($con);
//}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句