1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
$column_array = array( 'category' => array($cat, 'int'), 'title' => array($title, 'str'), 'client' => array($client, 'str'), 'duration_from' => array($from, 'date'), 'duration_to' => array($to, 'date'), 'description' => array($desc, 'str'), 'links' => array($links, 'str'), 'created_time' => array(date('Y-m-d G:i:s', time()), 'date'), 'updated_time' => array(date('Y-m-d G:i:s', time()), 'date') ); try{ $sql = "insert into research (category, title, client, duration_from, duration_to, description, links, " . " created_time, updated_time) values ( "; $sep = ''; foreach($column_array as $key => $value){ switch($value[1]){ case 'int': $sql .= $sep . $db->formatSQLOutput($value[0]); break; case 'str': $sql .= $sep . "'" . $db->formatSQLOutput($value[0]) . "'"; break; case 'date': $sql .= $sep . (strlen($value[0]) == 0 ? 'NULL' : "'" . $db->formatSQLOutput($value[0]) . "'"); break; } $sep = ', '; } $sql .= ")"; //echo $sql; //exit(); $result = mysql_query($sql, $db->link); if(!$result) throw new Exception("無法新增資料"); $insert_id = mysql_insert_id($db->link); $item_id = $insert_id; }catch(Exception $e){ $err = $e->getMessage(); $errCode = 6; } try{ $sql = 'update `research` set '; $sep = ''; foreach($column_array as $key => $value){ if($key == 'created_time') continue; switch($value[1]){ case 'int': $sql .= $sep . "`" . $key . "` = " . $db->formatSQLOutput($value[0]); break; case 'str': $sql .= $sep . "`" . $key . "` = '" . $db->formatSQLOutput($value[0]) . "'"; break; case 'date': $sql .= $sep . "`" . $key . "` = " . (strlen($value[0]) == 0 ? 'NULL' : "'" . $db->formatSQLOutput($value[0]) . "'"); break; } $sep = ', '; } $sql .= " where `id` = " . $item_id; //echo $sql; //exit(); $result = mysql_query($sql, $db->link); if(!$result) throw new Exception("Failed to update"); }catch(Exception $e){ $err = $e->getMessage(); $errCode = 7; } |