function process_request() { $item_id = isset($_POST['item_id']) ? intval($_POST['item_id']) : null; $item_name = isset($_POST['item_name']) ? $_POST['item_name'] : null; $item_price = isset($_POST['item_price']) ? $_POST['item_price'] : null; $item_description = isset($_POST['item_description']) ? $_POST['item_description'] : null; $item_img = isset($_POST['item_img']) ? $_POST['item_img'] : null; if (is_null($item_id) || $item_id <= 0) { die; } if (!is_null($item_name)) { $item_name = htmlspecialchars(trim($item_name)); if ($item_name === '') { die; } } if (!is_null($item_price)) { if (!preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) { die; } } if (!is_null($item_description)) { $item_description = htmlspecialchars(trim($item_description)); } $item = db_get_item($item_id); if (!$item) { die; } $values = []; if (!is_null($item_name)) { $values['name'] = $item_name; } if (!is_null($item_price)) { $item_price = str_replace(',', '.', $item_price); $values['price'] = $item_price; } if (!is_null($item_description)) { $values['description'] = $item_description; } if (!is_null($item_img)) { $values['imgurl'] = $item_img; } if (!empty($values)) { db_update_item($item_id, $values); $mc_handler = memcache_connect('localhost'); memcache_delete($mc_handler, get_page_cache_key($item_id)); $min_price = min($item_price, $item['price']); pagination_rebuild_ids($mc_handler, $item_id, 1); if ($item_price == $item['price']) { $edited_pages_amount = 1; } else { $edited_pages_amount = 0; } pagination_rebuild_prices($mc_handler, $min_price, $edited_pages_amount); pagination_rebuild_prices($mc_handler, $min_price); } header('Location: /view_item.php?id=' . $item_id); }
function process_request() { $item_id = isset($_POST['item_id']) ? intval($_POST['item_id']) : null; $item_name = isset($_POST['item_name']) ? $_POST['item_name'] : null; $item_price = isset($_POST['item_price']) ? $_POST['item_price'] : null; $item_description = isset($_POST['item_description']) ? $_POST['item_description'] : null; $item_img = isset($_POST['item_img']) ? $_POST['item_img'] : null; $errors = []; if (is_null($item_id) || $item_id <= 0) { $errors[] = 'Incorrect id'; } if (!is_null($item_name)) { $item_name = htmlspecialchars(trim($item_name)); if ($item_name === '') { $errors[] = 'Non-empty name required'; } } if (!is_null($item_price)) { if (!preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) { $errors[] = 'Incorrect price number'; } } if (!is_null($item_description)) { $item_description = htmlspecialchars(trim($item_description)); } if (!empty($errors)) { api_echo_as_json($errors, 'errors', RESPONSE_STATUS_FAIL); return; } $item = db_get_item($item_id); if (!$item) { api_echo_as_json("Item not found", 'msg'); return; } $values = []; if (!is_null($item_name)) { $values['name'] = $item_name; } if (!is_null($item_price)) { $item_price = str_replace(',', '.', $item_price); $values['price'] = $item_price; } if (!is_null($item_description)) { $values['description'] = $item_description; } if (!is_null($item_img)) { $values['imgurl'] = $item_img; } if (!empty($values)) { db_update_item($item_id, $values); $mc_handler = memcache_connect('localhost'); memcache_delete($mc_handler, get_page_cache_key($item_id)); pagination_rebuild_ids($mc_handler, $item_id, 1); $min_price = min($item_price, $item['price']); if ($item_price == $item['price']) { $edited_pages_amount = 1; } else { $edited_pages_amount = 0; } pagination_rebuild_prices($mc_handler, $min_price, $edited_pages_amount); } api_echo_as_json('Item successfully edited', 'msg'); }