douqiu1604 2013-05-11 05:45
浏览 15
已采纳

输入发送空值的无线电项目

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?

  • 写回答

1条回答 默认 最新

  • doucai5315 2013-05-11 06:24
    关注

    If I understand correctly try to change

    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>";
    

    to

    if(isset($_POST['parent_id']) && $_POST['parent_id']){
        $parent_id = $_POST['parent_id'];
        $res = mysql_query("INSERT INTO `category` (`parent_id`, `name`) VALUES('$parent_id','$name')");
    } else {
        $res = mysql_query("INSERT INTO `category` (`name`) VALUES('$name')");
    }
    
    if ($res) {
        echo "<center>Creaste la categoría correctamente!</center>";
    } else {
        echo "<center>Error creating a category!</center>";
    }
    

    and get rid of or comment $parent_id = $_POST['parent_id']; line at the beginning of your script

    $name = protect($_POST['name']);
    //$parent_id = $_POST['parent_id'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line