您好,我正在创建和无限子类别系统,一切都很好,除了创建新类别的代码......
根是“NULL”,所以我无法 <input type="radio" name="parent_id" value="">
在数据库中创建一个“写入”空值..
如果我不设置值,我会收到未定义索引错误。我尝试过 isset、empty、is_null 但它们似乎都有问题,
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
这是我的代码,所以你可以看到我想要做什么。
//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>";
}
}
}
}
该代码实际上可以工作并创建类别!
但是,如果我创建一个根类别,即使成功创建类别,它也会在“parent_id”上显示未定义索引的错误,
¿我如何摆脱这个错误?