我有一个动态矩阵框,用户可以无限制地使用药物,数据来自:
fopen64
我想要实现的是将每一行插入一个新行(MySQL)并插入到它们的相关列中,如下所示:
列:|药物|力量|表格|量
第1行 | Calpol | 100毫克|液体| 1
Row2 |扑热息痛| 200毫克|平板电脑| 16
我猜它使用爆炸功能> (我是新手)然后sql插入字符串?
答案 0 :(得分:1)
如果您将值作为json字符串集合,首先您需要爆炸然后字符串然后使用a for each循环遍历每个字符串,然后使用另一个来制作单行。请提供以下代码,这可能会对您有所帮助。
$addindividuals = '{"Drug":"Calpol","Strength":"100mg","Form":"Liquid","Quantity":"1"},{"Drug":"Paracetamol","Strength":"200mg","Form":"Tablet","Quantity":"16"}';
$exploded_array = explode('},',$addindividuals);
$final_query = "INSERT INTO `table_name` (`Drug`,`Strength`,`Form`,`Quantity`) VALUES ";
$exploded_array[0] = $exploded_array[0].'}';
foreach($exploded_array as $exploded_element)
{
$single_row = '(';
$json_decode = json_decode($exploded_element,true);
foreach($json_decode as $key => $value)
{
$single_row .= "'$value',";
}
$single_row = substr($single_row,0,-1);
$single_row .= '),';
$final_query .= $single_row;
}
$final_query = substr($final_query,0,-1);
echo $final_query;