Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Trying to access array offset on value of type bool in

Quando eu abro a pagina 'novo-video' aparece o seguinte warning: Warning: Trying to access array offset on value of type bool in C:\Users\User\Documents\Cursos\PHP\PHP na web\MVC\aluraplay\formulario.php on line 60

tentei resolver sozinho mas nao consegui

arquivo formulario.php:

<?php
    $pdo = new PDO('mysql:host=localhost;dbname=aluraplay', 'root', '123456');

    $id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);

    $video = [
        'url' => '',
        'title' => ''
    ];
    // var_dump($id);
    // exit();
    if($id !== false or null){

        $statement= $pdo->prepare("SELECT * FROM videos WHERE idvideos = ?");
        $statement->bindValue(1, $id, PDO::PARAM_INT);
        $statement->execute();
        $video = $statement->fetch(PDO::FETCH_ASSOC);
    }

?>

<!DOCTYPE html>
<html lang="pt-br">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../css/reset.css">
    <link rel="stylesheet" href="../css/estilos.css">
    <link rel="stylesheet" href="../css/estilos-form.css">
    <link rel="stylesheet" href="../css/flexbox.css">
    <title>AluraPlay</title>
    <link rel="shortcut icon" href="./img/favicon.ico" type="image/x-icon">
</head>

<body>

    <!-- Cabecalho -->
    <header>

        <nav class="cabecalho">
            <a class="logo" href="/"></a>

            <div class="cabecalho__icones">
                <a href="novo-video" class="cabecalho__videos"></a>
                <a href="../pages/login.html" class="cabecalho__sair">Sair</a>
            </div>
        </nav>

    </header>

    <main class="container">

        <form class="container__formulario" method="post">
            <h2 class="formulario__titulo">Envie um vídeo!</h2>
                <div class="formulario__campo">
                    <label class="campo__etiqueta" for="url">Link embed</label>
                    <input name="url" value="<?=$video['url'] ?>" class="campo__escrita" required
                        placeholder="Por exemplo: https://www.youtube.com/embed/FAY1K2aUg5g" id='url' />
                </div>


                <div class="formulario__campo">
                    <label class="campo__etiqueta" for="titulo">Titulo do vídeo</label>
                    <input name="title" value="<?=$video['title']?>" class="campo__escrita" required placeholder="Neste campo, dê o nome do vídeo"
                        id='titulo' />
                </div>

                <input class="formulario__botao" type="submit" value="Enviar" />
        </form>

    </main>

</body>

</html>

arquivo index.php:

<?php 

declare(strict_types=1);
// var_dump($_SERVER['PATH_INFO'], $_SERVER['REQUEST_METHOD']);
// exit();


if (!array_key_exists('PATH_INFO', $_SERVER) || $_SERVER['PATH_INFO'] === '/') {
    require_once 'listagem-videos.php';
} elseif ($_SERVER['PATH_INFO'] === '/novo-video') {
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
        require_once 'formulario.php';
    } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
        require_once 'novo-video.php';
    }
} elseif ($_SERVER['PATH_INFO'] === '/editar-video') {
    if ($_SERVER['REQUEST_METHOD'] === 'GET') {
        require_once 'formulario.php';
    } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
        require_once 'editar-video.php';
    }
} elseif ($_SERVER['PATH_INFO'] === '/remover-video') {
    require_once 'remover-video.php';
}   

arquivo: novo-video.php

<?php 

$pdo = new PDO('mysql:host=localhost;dbname=aluraplay', 'root', '123456');

$url = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL);
if($url == false){
    header('Location: /?sucesso=0');
    exit();
}

$title = filter_input(INPUT_POST, 'title');
if($title == false){
    header('Location: /?sucesso=0');
    exit();
}


$sql = 'INSERT INTO videos (url, title) VALUES (?,?);';
$statement = $pdo->prepare($sql);
$statement->bindValue(1, $url);
$statement->bindValue(2, $title);

header('Location: /');
1 resposta
solução!

Oii, Gabriel. Nesta aula aqui o professor resolve o problema:

https://cursos.alura.com.br/course/php-web-conhecendo-padrao-mvc/task/118308

No seu ìf tenta colocar assim a condição pra ver se resolve:

if ($id !== false && $id !== null)

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software