function addPlan($post) { $projectId = $post["projectId"]; $plans = getPlanByProjectId($projectId); $fields = array('conCleaHeatDefine', 'bottomDig', 'toiletBalCheck', 'plumbElecCheck', 'knockWall', 'woodProCheck', 'paintMateCheck', 'paintProConstruct', 'cabiInstall', 'tileMarbleCabiDefine', 'waterElecCheck', 'waterElecConstruct', 'waterElecPhoto', 'tilerMateConstruct', 'tilerProCheck', 'woodMateCheck', 'woodProConstruct', 'toilKitchSuspend', 'paintProCheck', 'switchSocketInstall', 'lampSanitInstall', 'floorInstall', 'paintRepair', 'wallpaperPave', 'housekeepingClean', 'elecInstall', 'curtainFuniInstall'); if (count($plans) != 0) { throw new Exception("plan with projectId:{$projectId} already exist!"); } $obj = array("id" => date("YmdHis") . str_pad(rand(0, 9999), 4, rand(0, 9), STR_PAD_LEFT), "projectId" => $projectId); foreach ($fields as $field) { if (isset($post[$field])) { $obj[$field] = $post[$field]; } } global $mysql; $mysql->DBInsertAsArray("`plan`", $obj); return array('status' => 'successful', 'errMsg' => '', 'planId' => $obj["id"]); }
<?php include_once "conn.php"; include_once "planDB.php"; $action = $_REQUEST["action"]; $res = ""; switch ($action) { case "addPlan": $res = addPlan($_POST); break; case "deletePlan": $res = deletePlan($_REQUEST["id"]); break; case "deletePlanByProjectId": $res = deletePlanByProjectId($_REQUEST["projectId"]); break; case "editPlan": $res = editPlan($_POST); break; case "getPlan": $res = getPlan($_REQUEST["id"]); break; case "getPlanByProjectId": $res = getPlanByProjectId($_REQUEST["projectId"]); break; default: throw new Exception("unknown action:" . $action); } echo json_encode($res);