douqu2712
douqu2712
2013-05-22 14:56

错误提示:未定义索引显示[关闭]

已采纳

I have three checkboxes ,the user should check one or 2 choices.

when I checked 2 choices an error message appear.

    $workshop1Day1 = $_POST["workshop1Day1"];
    $workshop2Day1 = $_POST["workshop2Day1"];
    $workshop3Day1 = $_POST["workshop3Day1"];


 $requete = "INSERT INTO Participant ( NameSurname,workshop1Day1,workshop2Day1,workshop3Day1)
                      VALUES ('$NameSurname', '$workshop1Day1', '$workshop2Day1', '$workshop3Day1')";
        $result = @mysql_query($requete);

error:

Notice: Undefined index: workshop3Day1

Then I have tried the following code (eg :in case the checkbox number 3 not checked),the same error appear

  $workshop1Day1=' ';
  $workshop2Day1='';
  $workshop3Day1='';

  $workshop1Day1 = $_POST["workshop1Day1"];
  $workshop2Day1 = $_POST["workshop2Day1"];
  $workshop3Day1 = $_POST["workshop3Day1"];

if (isset($workshop1Day1)&& isset($workshop2Day1)&& !isset($workshop3Day1))
{

    $requete = "INSERT INTO Participant ( NameSurname,workshop1Day1,workshop2Day1,workshop3Day1)
                  VALUES ('$NameSurname', '$workshop1Day1', '$workshop2Day1', '$workshop3Day1')";
    $result = @mysql_query($requete);

i know that isset is the solution but how?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzhun8615 douzhun8615 8年前

    You're getting the warning because you're not running isset on the source variable, just on what you assign it to.

    This line ($workshop3Day1 = $_POST["workshop3Day1"];) will raise the warning because that array index doesn't exist. Instead, use the isset on that line, then use the result of that in your if condition. Something like this:

    $workshop1Day1 = isset($_POST["workshop1Day1"]) ? $_POST["workshop1Day1"] : 0;
    $workshop2Day1 = isset($_POST["workshop2Day1"]) ? $_POST["workshop2Day1"] : 0;
    $workshop3Day1 = isset($_POST["workshop3Day1"]) ? $_POST["workshop3Day1"] : 0;
    

    Then remove the isset from your if condition.

    点赞 评论 复制链接分享