-2

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?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 2
    You get the error here: `$_POST["workshop3Day1"]`, because *it* is *not set*...! \*hint\*hint\*think\*about\*it!\* – deceze May 22 '13 at 14:59
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin May 22 '13 at 15:00
  • [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/) – deceze May 22 '13 at 15:00
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn May 22 '13 at 15:01
  • `$workshop3Day1` is set, you're are setting it to an empty string when you do this `$workshop3Day1 = $_POST["workshop3Day1"];` you should use empty – Orangepill May 22 '13 at 15:01
  • 1
    Checkboxes which are NOT checked in an html form are **NOT** submitted with the data, so you're trying to use a $_POST value that doesn't exist, hence your error. – Marc B May 22 '13 at 15:13

1 Answers1

3

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.

egrunin
  • 24,650
  • 8
  • 50
  • 93
slugonamission
  • 9,562
  • 1
  • 34
  • 41