0

I am creating two level dropdown select box with PHP, MYSQL and JQuery. Everything seems fine but when my parent category doesn't have child or if I doesn't select child it is giving me error and I can't understand what this error means and how to resolve. I need you guyz help.

Error: Notice: Undefined index: f_child_cat in C:\xampp\htdocs\pathtofile\index.php on line 58 You selected 12 & You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Here is my front end code

<?php if(isset($_POST['submit'])){
    $drop = $_POST['first_child_cat'];
    $f_child_cat = $_POST['f_child_cat'];
    echo "You selected ";
    echo $drop." & ".$f_child_cat;

    $query =    mysql_query("SELECT * FROM qa_categories WHERE parentid = $f_child_cat")
                or die(mysql_error());

                echo '<ul>';
                while($cats = mysql_fetch_array( $query ))
                    echo '<li>',$cats['title'],'</li>';

                echo '</ul>';
}
?>
Code Lover
  • 8,099
  • 20
  • 84
  • 154

2 Answers2

1

$_POST['f_child_cat'] is not defined check if the value is present first before using it.

$f_child_cat = isset($_POST['f_child_cat'])?$_POST['f_child_cat']:false;

if ($f_child_cat){
    $f_child_cat = mysql_real_escape_string($f_child_cat); // always escape

    /// your query.
}

Note: mysql_* functions are depricated, use mysqli or pdo as an alternative.

Ibu
  • 42,752
  • 13
  • 76
  • 103
  • This is great but just to understand is your code is better or`Polish Prince` code. Please don't take me wrong but I am not so master so trying to understand from you experts... because both codes just works fine.. – Code Lover Mar 22 '13 at 18:46
  • all you need to do is to **check** if the index is defined, hence `isset($_POST['f_child_cat'])` this will prevent errors; – Ibu Mar 22 '13 at 18:49
  • @Ibu `$_POST['f_child_cat']` can be defined but can be empty. – Kermit Mar 22 '13 at 18:51
  • Oh I got it! thanks a lot to learn me.. really appreciate – Code Lover Mar 22 '13 at 18:52
1

It means that $_POST['f_child_cat'] is not defined (doesn't exist). You should be using binded parameters to prevent against SQL injection and use mysqli_ or PDO functions.

Since this is part of your query, I would recommend that you wrap your query like so:

if( isset($_POST['f_child_cat']) && !empty($_POST['f_child_cat']) ) {
    ... call db
}
else {
    echo "You didn't select this"
}
Kermit
  • 33,827
  • 13
  • 85
  • 121
  • Yes that I only understood but than how to resolve this error? As this is an optional and if have category than it will available in dropdown else not than when user submit it gives error. Also if user by chance doesn't select second level category than also gives error.. how to resolve? – Code Lover Mar 22 '13 at 18:41
  • @pixelngrain You have to answer the question of what you expect the code to do if it's empty since you're sending it as part of your query. See my update. – Kermit Mar 22 '13 at 18:42
  • Thanks a lot for your support and your and `Ibu` both code works just fine.. just want to know which is better? also one quick related question if child cat doesn't exists in database than how can I NOT display second dropdown? – Code Lover Mar 22 '13 at 18:48
  • @pixelngrain Ibu's answer does not check if `$_POST['f_child_cat']` is empty (it can be defined but can be empty) – Kermit Mar 22 '13 at 18:52
  • Hmm..! I have used your answer and works compeletly fine with my code.. Thanks a lot.. – Code Lover Mar 22 '13 at 18:53