Error Undefined variable php [closed]

2

I have a page that looks for the products in the database and displays me in the index through an include ie index.php shows the include with the products. I have in the include an option to search by name the products in the bank. as it arrows separate search page I do not know how to make it open in the index. so soon I brought the search values in the bank to the page giving the include the problem ta being the following: On the same page he is bringing the value of the bank's products to display and tbm bringing the value of the search of the products. I think it's rolling the conflict pos works showing the error message in the index. but if I search it brings the result and it gets normal without error.

<?php include_once("conexao_produtos.php");
//Verificar se está sendo passado na URL a página atual, senao é atribuido a pagina 
$pagina = (isset($_GET['pagina']))? $_GET['pagina'] : 1;
if(!isset($_GET['pesquisar'])){
}else{
    $valor_pesquisar = $_GET['pesquisar'];
}

/* TRAZ OS PRODUTOS DO BANCO PARA SEREM MOSTRADOS */
//Selecionar todos os cursos da tabela
$result_curso = "SELECT * FROM produtos WHERE prod_nome LIKE '%$valor_pesquisar%'";
$resultado_curso = mysqli_query($conn, $result_curso);

//Contar o total de cursos
$total_cursos = mysqli_num_rows($resultado_curso);

//Seta a quantidade de cursos por pagina
$quantidade_pg = 6;

//calcular o número de pagina necessárias para apresentar os cursos
$num_pagina = ceil($total_cursos/$quantidade_pg);

//Calcular o inicio da visualizacao
$incio = ($quantidade_pg*$pagina)-$quantidade_pg;
/* TRAZ OS PRODUTOS DO BANCO PARA SEREM MOSTRADOS EM CASO DE BUSCA */
//Selecionar os cursos a serem apresentado na página
$result_cursos = "SELECT * FROM produtos WHERE prod_nome LIKE '%$valor_pesquisar%' limit $incio, $quantidade_pg";
$resultado_cursos = mysqli_query($conn, $result_cursos);
$total_cursos = mysqli_num_rows($resultado_cursos);
?>

As he introduces me that he looks like an error on line 17 and 33 to 17 I have the result of the products:

$result_curso = "SELECT * FROM produtos WHERE prod_nome LIKE '%$valor_pesquisar%'";
insira o código aqui

On the 33rd of the survey.

$result_cursos = "SELECT * FROM produtos WHERE prod_nome LIKE '%$valor_pesquisar%' limit $incio, $quantidade_pg";
    
asked by anonymous 23.01.2017 / 00:55

1 answer

0

That's a silly thing, if that's what I think is happening. It happens because sometimes the variable with the value to be searched, sometimes will not exist in some contexts, but it is loaded in the same way. A simple solution is to check if it exists before assigning the value like this:

//passo o valor para a variavel sómente se o get existir
$valor_pesquisar = isset($_GET['pesquisar'])?isset($_GET['pesquisar']):'';

In case you checked it, you only checked it when it exists, and when not, it simply did not do anything, so if it does not exist, your variable will not exist either, ie your if was not worth anything . Another solution would be to put all your search inside the other there ... I hope I have helped

    
23.01.2017 / 01:17