Undefined variable php error

Asked

Viewed 149 times

2

I have a page that searches the products in the database and shows me in the index through a include or index.php shows include with the products.
have no include an option to search by name the products in the bank. as it separate page search arrow I don’t know how to make it open on index. so soon brought the search values in the bank to the page giving include the problem ta being the following: On the same page it is bringing the value of the bank products to display and also bringing the value of product search. I believe it is rolling the pos conflict works by showing the error message in index. but if I search it brings the result and is 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 presents to me that you are seeming error on line 17 and on line 33 to 17 got the result of the products :

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

In 33 the research.

$result_cursos = "SELECT * FROM produtos WHERE prod_nome LIKE '%$valor_pesquisar%' limit $incio, $quantidade_pg";
  • has this kind of problem if you put two Results on the same page?

  • Leo, I read and read and I don’t understand the problem you’re having...

  • I am with these two Results ai placed in the same tag in php they are presenting me as if they were in trouble but are displaying normal even with the message Notice: Undefined variable: valor_search in C: Program Files Easyphp-Devserver-16.1 eds-www imperionutry index_produtos.php on line 17 and tbm on line 33 but if I do a search it stops presenting me the error message and displays normal but only with the result of the search that in the case is Lina 33

  • but the results are showing normal?

  • showed yes normal friend helped I had not passed the parameters to him in case there was no search made as if it existed.

1 answer

0


That’s kind of silly, if that’s what I think is going on. It happens because sometimes the variable with the value to be searched, sometimes it 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 thus:

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

In the case there Cvoce made the check, Cvoce came only when it exists, and when not, just did nothing, so if it does not exist, its variable will also not exist, ie your if not worth anything. Another solution would be to put all your research inside the Else there... I hope I’ve helped

  • That’s right, bro Obg,

  • 1

    Okay buddy, you helped a lot.

  • @Leoandrade You’re welcome to help :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.