複数の日付をDBにUPDATEする場合、レコードの値をそれぞれ別の値に置き換える処理についての質問です。
下記if文(if($delflg == true && $saveflg == true){...)でDBにある「is_deleted」カラムの値(削除フラグ)が「y」のものを「n」に、「n」であれば「y」に置き換えてそれぞれUPDATEしたいのですが、行き詰まっております。つまり、if文の条件が「$delflg == true && $saveflg == true」の場合のコードの間違いを修正したいです。どう修正すればよいかどなたか教えて頂けますでしょうか。ちなみに、else ifの中の処理は正常に行われています。
回答するのに情報が足りない場合は追加いたしますので、コメントを頂けたら幸いです。
よろしくお願い致します。
// 登録済であるか確認 $sql = "select count(*) as cnt from m_calendar where earth_holiday = '%%n%%'"; $ret = f_db_select($sql, $para['earth_holiday']); $h = array_shift($ret); $h2 = array_shift($ret2); $cnt = $h['cnt']; // calendartbl を更新 $sql = "select holiday, is_deleted from calendartbl where holiday in (%%holiholiday%%)"; $holiday_array = explode(',',$para['holiday']); for ($tt=0; $tt<count($holiday_array); $tt++) { $holiholiday1 .= "'".$holiday_array[$tt]."'".","; } $holiholiday1 = rtrim($holiholiday1, ","); $sql = preg_replace('/%%holiholiday%%/', $holiholiday1, $sql); $ret = f_db_select($sql); $ret2 = f_db_select($sql); $tobe = ""; $tobe2 = ""; //(var_dumpは下部に掲載してあります) var_dump($ret); var_dump($sql); for($ss=0; $ss<count($holiday_array); $ss++){ foreach ($ret as $h){ if($h['holiday'] == $holiday_array[$ss]){ if($h['is_deleted'] == "n"){ $delflg = true; }else if($h['is_deleted'] == "y"){ $saveflg = true; } } } if ($ret == NULL){ $newflg = true; } } if($delflg == true && $saveflg == true){ if($h['is_deleted'] == "n"){ $tobe = "y"; foreach( $holiday_array as $key => $value ) { $sql = "update calendartbl set is_deleted = '%%n%%' where holiday = '%%n%%'"; $ret2 = f_db_update($sql, $tobe, $value); print("aaa1"); } }else if($h['is_deleted'] == "y"){ $tobe = "n"; foreach( $holiday_array as $key => $value ) { $sql = "update calendartbl set is_deleted = '%%n%%' where holiday = '%%n%%'"; $ret2 = f_db_update($sql, $tobe, $value); print("aaa2"); } } }else if($delflg == true){ $tobe = "y"; foreach( $holiday_array as $key => $value ) { $sql = "update calendartbl set is_deleted = '%%n%%' where holiday = '%%n%%'"; $ret2 = f_db_update($sql, $tobe, $value); print("bbb"); } }else if($saveflg == true){ $tobe = "n"; foreach( $holiday_array as $key => $value ) { $sql = "update calendartbl set is_deleted = '%%n%%' where holiday = '%%n%%'"; $ret2 = f_db_update($sql, $tobe, $value); print("ccc"); } }else if($newflg == true){ $tobe2 = "n"; foreach( $holiday_array as $key2 => $value2 ) { $sql = "insert IGNORE into calendartbl (holiday, is_deleted) values ('%%n%%', '%%n%%')"; $ret = f_db_update($sql, $value2, $tobe2); print("ddd"); } }else{ $tobe2 = "n"; foreach( $holiday_array as $key2 => $value2 ) { $sql = "insert IGNORE into calendartbl (holiday, is_deleted) values ('%%n%%', '%%n%%')"; $ret = f_db_update($sql, $value2, $tobe2); print("eee"); } }
DB
-- Table "calendartbl" DDL CREATE TABLE `calendartbl` ( `holiday` date NOT NULL DEFAULT '0000-00-00' COMMENT '休日年月日', `is_deleted` char(1) NOT NULL COMMENT '削除フラグ(y, n)', PRIMARY KEY (`holiday`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='休日';
var_dump
【新規日付の場合】 var_dump($ret); array(0) { } var_dump($sql); string(99) "select earth_holiday, is_deleted from m_calendar where earth_holiday in ('2016-03-08','2016-03-09') 【新規ではなくDBにある場合】 var_dump($ret); array(2) { [0]=> array(4) { [0]=> string(10) "2016-03-08" ["earth_holiday"]=> string(10) "2016-03-08" [1]=> string(1) "y" ["is_deleted"]=> string(1) "y" } [1]=> array(4) { [0]=> string(10) "2016-03-09" ["earth_holiday"]=> string(10) "2016-03-09" [1]=> string(1) "y" ["is_deleted"]=> string(1) "y" } } var_dump($sql); string(99) "select earth_holiday, is_deleted from m_calendar where earth_holiday in ('2016-03-08','2016-03-09')
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/08 05:46
2016/07/08 05:59
2016/07/08 06:33