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:
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!
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.