6 Replies - 7293 Views - Last Post: 31 July 2013 - 10:26 PM Rate Topic: -----

#1 EmiT FliX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-August 12

Notice: Undefined index - This is puzzling me.

Posted 07 August 2012 - 11:31 PM

Hey,

Problem with either my PHP or MySQL (I am pretty sure it's my php), So I'm working on my websites login system (the user must be logged in to view anything on the site.) and than all of a sudden for my registry page I have this amazing (sarcasm), Notice come up. Now I know notices can be hidden, But, I really prefer knowing there's no errors or notices and it will just plain and simply run without me having to worry with a user logging in/registering.

So here it comes. My Code So far.
<?php
// dbConfig.php is a file that contains your
// database connection information. This
// tutorial assumes a connection is made from
// this existing file.
include ("dbConfig.php");
//Input vaildation and the dbase code
[b]if ( $_GET["op"] == "reg" )[/b]
 {
 $bInputFlag = false;
 foreach ( $_POST as $field )
  {
  if ($field == "")
   {
   $bInputFlag = false;
   }
  else
   {
   $bInputFlag = true;
   }
  }
 // If we had problems with the input, exit with error
 if ($bInputFlag == false)
  {
  die( "Problem with your registration info. "
   ."Please go back and try again.");
  }
 // Fields are clear, add user to database
 //  Setup query
 $q = "INSERT INTO `dbusers` (`username`,`password`,`email`) "
  ."VALUES ('".$_POST["username"]."', "
  ."PASSWORD('".$_POST["password"]."'), "
  ."'".$_POST["email"]."')";
 //  Run query
 $r = mysql_query($q);
 
 // Make sure query inserted user successfully
 if ( !mysql_insert_id() )
  {
  die("Error: User not added to database.");
  }
 else
  {
  // Redirect to thank you page.
  Header("Location: register.php?op=thanks");
  }
 } // end if
//The thank you page
[b]elseif ( $_GET["op"] == "thanks" )[/b]
 {
 echo "<h2>Thanks for registering!</h2>";
 }
 
//The web form for input ability
else
 {
 echo "<form action=\"?op=reg\" method=\"POST\">\n";
 echo "Username: <input name=\"username\" MAXLENGTH=\"16\">\n";
 echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\">\n";
 echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\">\n";
 echo "<input type=\"submit\">\n";
 echo "</form>\n";
 }
// EOF
?>



The bold is to help you with getting straight to the code that has a notice on it.

Also these are the specific Notices.
Notice: Undefined index: op in C:\xampp\htdocs\Website\Website\register.php on line 14

Notice: Undefined index: op in C:\xampp\htdocs\Website\Website\register.php on line 55

I have looked at a couple of other forums that have had the "op" issue and they say to put an "isset" before the op, but to no luck doesn't work in my case.

Thanks in advanced,
FliX

One addition, this was a copy paste job from a site that may be outdated (I had set up all the database and stuff so that shouldn't be a problem) link can be provided if you need it.

Is This A Good Question/Topic? 0
  • +

Replies To: Notice: Undefined index - This is puzzling me.

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Notice: Undefined index - This is puzzling me.

Posted 07 August 2012 - 11:36 PM

View PostEmiT FliX, on 08 August 2012 - 08:28 AM, said:

I have looked at a couple of other forums that have had the "op" issue and they say to put an "isset" before the op, but to no luck doesn't work in my case.

then youíre doing it wrong. isset() is exatly for that purpose:
if ( isset($_GET["op"]) and ($_GET["op"] == "reg") )

alternately you could use filter functions, though they make more sense with non-string data.
$op = filter_input(INPUT_GET, "op");

Was This Post Helpful? 0
  • +
  • -

#3 EmiT FliX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-August 12

Re: Notice: Undefined index - This is puzzling me.

Posted 07 August 2012 - 11:46 PM

Thanks Dormilich, I seen you reading my post (yes that seems stalker like.), so this code you propose (mainly the filter), what is the difference opposed to just using isset()? (trying to learn as I go instead of just copy pasting code, which seems hypocritical as I copied the original Registration code from a website (it was more laziness than anything.))
Was This Post Helpful? 0
  • +
  • -

#4 EmiT FliX   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-August 12

Re: Notice: Undefined index - This is puzzling me.

Posted 08 August 2012 - 12:02 AM

Ok, so, attempt number one is a no-go (using the filter_input) this is what I achieved, I was rid of the Notices for the "op" problem but than I'm guessing it's something wrong with my "Thank you page" part. Here is the code + the new problem.

<?php
//The thank you page
$op = filter_input(INPUT_GET, "op" == "thanks");
{ 

 echo "<h2>Thanks for registering!</h2>";
 }
 
//The web form for input ability
else
 {
 echo "<form action=\"?op=reg\" method=\"POST\">\n";
 echo "Username: <input name=\"username\" MAXLENGTH=\"16\">\n";
 echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\">\n";
 echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\">\n";
 echo "<input type=\"submit\">\n";
 echo "</form>\n";
 }
// EOF
?>

The Error:
Parse error: syntax error, unexpected 'else' (T_ELSE) in C:\xampp\htdocs\Website\Website\register.php on line 62




Addition:
Using
if ( isset($_GET["op"])
worked for my first line that I had the problem with.

Trying to use the (isset) for my other line is not working so much...

Problem solved, I now have no Notices, I had apparently put an extra "'" in there so I went back over it and it works perfectly, I guess I was using (isset) wrongly before.

Thanks Dorm.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Notice: Undefined index - This is puzzling me.

Posted 08 August 2012 - 12:04 AM

View PostEmiT FliX, on 08 August 2012 - 08:59 AM, said:

Ok, so, attempt number one is a no-go (using the filter_input) this is what I achieved, I was rid of the Notices for the "op" problem but than I'm guessing it's something wrong with my "Thank you page" part. Here is the code + the new problem.

<?php
//The thank you page
$op = filter_input(INPUT_GET, "op" == "thanks");


well, if you change the parameters to an invalid set, then itís bound not to work.
mixed filter_input ( int $type , string $variable_name [, int $filter = FILTER_DEFAULT [, mixed $options ]] )

Was This Post Helpful? 0
  • +
  • -

#6 AlpeshRathod   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-July 13

Re: Notice: Undefined index - This is puzzling me.

Posted 31 July 2013 - 09:16 PM

View PostDormilich, on 08 August 2012 - 12:04 AM, said:

View PostEmiT FliX, on 08 August 2012 - 08:59 AM, said:

Ok, so, attempt number one is a no-go (using the filter_input) this is what I achieved, I was rid of the Notices for the "op" problem but than I'm guessing it's something wrong with my "Thank you page" part. Here is the code + the new problem.

<?php
//The thank you page
$op = filter_input(INPUT_GET, "op" == "thanks");


well, if you change the parameters to an invalid set, then itís bound not to work.
mixed filter_input ( int $type , string $variable_name [, int $filter = FILTER_DEFAULT [, mixed $options ]] )


It should be like this if( $op = filter_input(INPUT_GET, "op" == "thanks"))
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Notice: Undefined index - This is puzzling me.

Posted 31 July 2013 - 10:26 PM

View PostAlpeshRathod, on 01 August 2013 - 06:16 AM, said:

It should be like this if( $op = filter_input(INPUT_GET, "op" == "thanks"))

false (i.e. 0) is certainly not a valid parameter name.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1