Insert & Update Template

	$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;
	}