function process_request()
{
    $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_name)) {
        die;
    } else {
        $item_name = htmlspecialchars(trim($item_name));
        if ($item_name === '') {
            die;
        }
    }
    if (is_null($item_price) || !preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) {
        die;
    }
    $item_price = str_replace(',', '.', $item_price);
    if (is_null($item_description)) {
        die;
    } else {
        $item_description = htmlspecialchars(trim($item_description));
    }
    if (is_null($item_img)) {
        $item_img = "Null";
    }
    $id = db_insert_item($item_name, $item_description, $item_price, $item_img);
    $mc_handler = memcache_connect('localhost');
    if (memcache_get($mc_handler, 'total_rows') !== false) {
        memcache_increment($mc_handler, 'total_rows');
        pagination_rebuild_ids($mc_handler, $id);
        pagination_rebuild_prices($mc_handler, $item_price);
    }
    header('Location: /view_item.php?id=' . $id);
}
Beispiel #2
0
function process_request()
{
    $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_name)) {
        $errors[] = 'Non-empty name required';
    } else {
        $item_name = htmlspecialchars(trim($item_name));
        if ($item_name === '') {
            $errors[] = 'Non-empty name required';
        }
    }
    if (is_null($item_price) || !preg_match("/^\\d+([.,]\\d{1,2})?\$/", $item_price)) {
        $errors[] = 'Incorrect price number';
    }
    if (is_null($item_description)) {
        $errors[] = 'Incorrect description';
    } else {
        $item_description = htmlspecialchars(trim($item_description));
    }
    if (!empty($errors)) {
        api_echo_as_json($errors, 'errors', RESPONSE_STATUS_FAIL);
        return;
    }
    $item_price = str_replace(',', '.', $item_price);
    if (is_null($item_img)) {
        $item_img = "Null";
    }
    db_insert_item($item_name, $item_description, $item_price, $item_img);
    $mc_handler = memcache_connect('localhost');
    if (memcache_get($mc_handler, 'total_rows') !== false) {
        memcache_increment($mc_handler, 'total_rows');
    }
    api_echo_as_json('Item created', 'msg');
}