私は次の値を持つ次の表を持っています:
id name farmid amount cycleid totalprice quantity month year
1 DAP 1 2400 1 24000 10 DECEMBER 2012
2 UREA 1 2500 1 5000 2 DECEMBER 2012
3 SENCOR 1 1200 1 2400 2 DECEMBER 2012
4 DAP 2 2400 1 2400 1 DECEMBER 2012
5 UREA 2 2500 1 2400 1 DECEMBER 2012
6 SENCOR 3 2000 2 4000 2 DECEMBER 2012
7 DAP 1 3000 2 3000 1 JANUARY 2013
8 UREA 2 3000 2 6000 2 JANUARY 2013
9 SENCOR 3 3000 2 6000 2 JANUARY 2013
バッチ更新を実行するための適切なSQLステートメントの作成に問題があります。たとえば、DECEMBER
年の月で2012
、farmidのみで行を更新します1
。注:farmid行は、同じ月、たとえば12月に複数のcycleidを持つことはできません。上記の問題に対する正しいSQLステートメントを考え出すのを手伝ってください。これは私がcode-igniterで試したものです:
$chemicalarray = array(
1=>array('name'=>$B41,'farmid'=>$farmname_id,'amount'=>$D41,'cycleid'=>$cycleid,'totalprice'=>$E41,'quantity'=>$C41,'year'=>$E4,'month'=>$E5),
2=>array('name'=>$B42,'farmid'=>$farmname_id,'amount'=>$D42,'cycleid'=>$cycleid,'totalprice'=>$E42,'quantity'=>$C42,'year'=>$E4,'month'=>$E5),
3=>array('name'=>$B43,'farmid'=>$farmname_id,'amount'=>$D43,'cycleid'=>$cycleid,'totalprice'=>$E43,'quantity'=>$C43,'year'=>$E4,'month'=>$E5),
4=>array('name'=>$B44,'farmid'=>$farmname_id,'amount'=>$D44,'cycleid'=>$cycleid,'totalprice'=>$E44,'quantity'=>$C44,'year'=>$E4,'month'=>$E5),
5=>array('name'=>$B45,'farmid'=>$farmname_id,'amount'=>$D45,'cycleid'=>$cycleid,'totalprice'=>$E45,'quantity'=>$C45,'year'=>$E4,'month'=>$E5),
);
foreach ($chemicalarray as $key) {
$unitsql = "UPDATE chemical SET name = '{$key['name']}', farmid = '{$key['farmid']}' ,
amount = '{$key['amount']}' , cycleid = '{$key['cycleid']}' ,
totalprice = '{$key['totalprice']}', quantity = '{$key['quantity']}'
WHERE year = '{$key['year']}'
AND month = '{$key['month']}'";
$q = mysql_query($unitsql);
echo $key['name'];
echo $key['amount'];
if($q==true){ }else{ echo mysql_error(); }
}
Output of var_dump is :
array(8) { ["name"]=> string(8) "Round Up" ["farmid"]=> string(1) "1" ["amount"]=> float(1300) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(15600) ["quantity"]=> float(12) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" } Done11array(8) { ["name"]=> string(7) "Krismat" ["farmid"]=> string(1) "1" ["amount"]=> float(1500) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(15000) ["quantity"]=> float(10) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" } Done11array(8) { ["name"]=> string(3) "DAP" ["farmid"]=> string(1) "1" ["amount"]=> float(2500) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(75000) ["quantity"]=> float(30) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" } Done11array(8) { ["name"]=> string(4) "Urea" ["farmid"]=> string(1) "1" ["amount"]=> float(2000) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(50000) ["quantity"]=> float(25) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" } Done11array(8) { ["name"]=> string(9) "Dual Gold" ["farmid"]=> string(1) "1" ["amount"]=> float(2500) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(75000) ["quantity"]=> float(30) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" }