Undefined index in php when trying to output the subcategories of each category from a phpmyadmin table

php mysql phpmyadmin localhost undefined-index

211 просмотра

1 ответ

I have 2 tables in phpmyadmin.I output the category names from this table like this in php:

<?php
$query="select * from category " ; 
$res=mysqli_query($conn,$query); 
while($row=mysqli_fetch_assoc($res))
{
  echo '<li>
      <a href="subcat.php?cat='.$row[ 'cat_id']. '&catnm='.$row[ "cat_nm"]. '">'.$row[ "cat_nm"]. '
      </a>
      </li>'; 
}
mysqli_close($conn);
?>

Then if the user clicks at one of these categories a list of subcategories which belong to the category clicked by the user will be outputed from my subcat table. The foreign key for subcat table is cat_id. An error occures when I try to output the subcategories like this:

<?php
$q = "select * from subcat  where parent_id = ".$_GET['cat'];//line 6
$cat=$_GET['cat_nm'];//line 8
$res = mysqli_query($conn,$q) or die("Can't Execute Query..");
$row1 = mysqli_fetch_assoc($res);
do
  {
    echo '<li>'.$row1['subcat_nm'].'</li>';
   }
while($row1 = mysqli_fetch_assoc($res))
?>

The error outputed is: enter image description here The cat and cat_nm are columns name of category table.Have I done something wrong here? Which is the right way to output the subcategories? Thanks!

Автор: coolest Источник Размещён: 08.11.2019 11:20

Ответы (1)


1 плюс

Решение

Because,

$_GET['cat_nm'] != $_GET['catnm']

Look at your link,

<a href="subcat.php?cat='.$row[ 'cat_id']. '&catnm='.$row[ "cat_nm"]. '">'.$row[ "cat_nm"]. '

--------------------------------------------^^^^^^

It should be $_GET['catnm'].

ProTip

You should be validating your user input and using prepared statements to prevent SQL Injection.

Автор: Script47 Размещён: 20.08.2016 12:54
Вопросы из категории :
32x32