PHP在数据库中的时间保存数据分成两行? [英] php save data into two rows at a time in database?
本文介绍了PHP在数据库中的时间保存数据分成两行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的形式
<窗体类=产品资料行动=>
<表>
< TR类=数据行>
&所述; TD>
<输入类型=数字NAME =金融[A] [source_unit]>
< / TD>
&所述; TD>
<输入类型=数字NAME =金融[A] [target_unit]>
< / TD>
&所述; TD>
<输入类型=数字NAME =金融[A] [client_price]>
< / TD>
&所述; TD>
<输入类型=数字NAME =金融[A] [client_salary]>
< / TD>
< / TR>
< TR类=数据行>
&所述; TD>
<输入类型=数字NAME =金融[B] [source_unit]>
< / TD>
&所述; TD>
<输入类型=数字NAME =金融[B] [target_unit]>
< / TD>
&所述; TD>
<输入类型=数字NAME =金融[B] [client_price]>
< / TD>
&所述; TD>
<输入类型=数字NAME =金融[B] [client_salary]>
< / TD>
< / TR>
< /表>
< /表及GT;
在这里你可以看到我有两行。一个用于和另一个为湾现在我想将它们保存在数据库中有两行。一用于在同一时间的一个,另一个用于湾当我做的print_r(金融)。与此code
$财政= $ _ POST ['金融'];
的print_r($财政);
它给我看一个这样的数组
阵列
(
[α] =>排列
(
[source_unit] => 3213
[target_unit] => 657654322343
[client_price] => 5435.00
[client_salary] => 897.00
) [α] =>排列
(
[source_units] => 67656565
[target_units] => 43243
[client_price] => 23432.00
[client_salary] => 6546.00
))
现在有人可以告诉我如何将它们保存每个一行。我有我的数据库表是这样的,数据应该保存这样
编号,产品类型,source_unit,target_unit,client_price,lient_salary
1一3213 657654322343 5435 897
2 B 67656565 43243 23432 6546
解决方案
环通阵列,并且插入或相应更新。
的foreach($财政为$关键=> $数据)
{
//基于$键如果数据库更新插入其他存在价值
回声$键'< BR />'。
回声$数据['source_unit']。'< BR />';
回声$数据['target_unit']'< BR />'。
呼应'<小时/>';
}
I have a form like this
<form class="product-data" action="">
<table>
<tr class="data-row">
<td>
<input type="number" name="finance[a][source_unit]" >
</td>
<td >
<input type="number" name="finance[a][target_unit]">
</td>
<td>
<input type="number" name="finance[a][client_price]">
</td>
<td>
<input type="number" name="finance[a][client_salary]" >
</td>
</tr>
<tr class="data-row">
<td>
<input type="number" name="finance[b][source_unit]" >
</td>
<td >
<input type="number" name="finance[b][target_unit]">
</td>
<td>
<input type="number" name="finance[b][client_price]">
</td>
<td>
<input type="number" name="finance[b][client_salary]" >
</td>
</tr>
</table>
</form>
here you can see I have two rows. One for a and another for b. Now I want to save them in the database with two rows. One for the a and another for b at a same time. When I am doing print_r(finance). with this code
$finances = $_POST['finance'];
print_r($finances);
Its showing me an array like this
Array
(
[a] => Array
(
[source_unit] => 3213
[target_unit] => 657654322343
[client_price] => 5435.00
[client_salary] => 897.00
)
[a] => Array
(
[source_units] => 67656565
[target_units] => 43243
[client_price] => 23432.00
[client_salary] => 6546.00
)
)
Now can someone tell me how to save them in each row. I have my database table is like this and the data should be saved like this
Id, product_type, source_unit, target_unit, client_price, lient_salary
1 A 3213 657654322343 5435 897
2 B 67656565 43243 23432 6546
解决方案
loop thru your array and either insert or update accordingly.
foreach($finances as $key => $data)
{
//based on the $key if value exists in database update else insert
echo $key.'<br />';
echo $data['source_unit'].'<br />';
echo $data['target_unit'].'<br />';
echo '<hr />';
}
这篇关于PHP在数据库中的时间保存数据分成两行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文