Hello im creating and infinite subcategory system, everything its fine except for the code to create new categories...
The Root is "NULL", so i cant manage to create a <input type="radio" name="parent_id" value="">
That "write" a null value in the database..
If i dont set the value, i get an Undefined Index error.. ihave tried isset, empty, is_null but all of them seems to have problems,
isset($var) is FALSE
empty($var) is TRUE
is_null($var) is TRUE
isset($novar) is FALSE
empty($novar) is TRUE
is_null($novar) gives an Undefined variable error
Here is my code so you can see what im trying to do.
//Check to see if the form has been submitted
if(isset($_POST['submit'])){
//protect and then add the posted data to variables
$name = protect($_POST['name']);
$parent_id = $_POST['parent_id'];
//check to see if a name was set
if(!$name){
//if any weren't display the error message
echo "<center>Necesitas llenar todos los campos</center>";
}else{
//Check if the wanted name is more than 99 or less than 2 charcters long
if(strlen($name) > 99 || strlen($name) < 2){
//if it is display error message
echo "<center>La categoría no puede rebasar los 100 caracteres!</center>";
}else{
if(is_null($parent_id)){
$res = mysql_query("INSERT INTO `category` ( `name`) VALUES('".$name."')");
echo "<center>Creaste la categoría correctamente!</center>";
}
else {
$res = mysql_query("INSERT INTO `category` (`parent_id`, `name`) VALUES('".$parent_id."','".$name."')");
echo "<center>Creaste la categoría correctamente!</center>";
}
}
}
}
The code actually works & create categories!
But if i create a root category, it shows an error of undefined index on "parent_id" even when creating the category successfully,
¿how do i get rid of that error?