0
<?php
if (isset($_POST['name'])) {$name = $_POST['name'];}
if (isset($_POST['email'])) {$email = $_POST['email'];}
if (isset($_POST['password'])) {$pass = $_POST['password'];}
require "init.php";

$query = "select * from userinfo where email like '".$email."";
$result = mysqli_query($con,$query);

if(mysqli_num_rows($result)>0)
{

$response = array();
$code = "reg_false";
$message = "User Already Exist !";
array_push($response,array("code"=>$code,"message"=>$message));
echo json_encode(array("server_response"=>$response));

}
   else
    {
    $query = "insert into userinfo     values('".$name."','".$email."','".$pass."');";
$result = mysqli_query($con,$query);

if(!$result)
{
    $response = array();
    $code = "reg_false";
    $message = "Registration Failed, Try Again!";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode(array("server_response"=>$response));
}
else
{
    $response = array();
    $code = "reg_true";
    $message = "Registration Success, Login to Continue!";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode(array("server_response"=>$response));
  } 
}
mysqli_close($con);
?>

I have tried almost everything on Stack Overflow but still I find errors as undefined variable also undefined index.#

Check this screen shot

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
  • @RajdeepPaul I read that post but nothing works thats y i questioned –  Jan 22 '17 at 08:31
  • Let's start with with the first *undefined $email* error, `$_POST['email']` is not set. Check whether email input field has a name named `email`. – Rajdeep Paul Jan 22 '17 at 08:37
  • @RajdeepPaul yah its there –  Jan 22 '17 at 09:34
  • Can you also provide your form code? – Gynteniuxas Jan 22 '17 at 10:02
  • @EdvinTenovimas I am using this in Android Studio –  Jan 22 '17 at 13:56
  • Oh, then it might be different from what I thought. Can't tell for sure. – Gynteniuxas Jan 22 '17 at 14:16
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 22 '17 at 19:32
  • Yes `$email`, `$name` and `$pass` will be undefined if they are not set, because you declare them inside an `if` statement. If those test don't become true, the variables then become undefined. – Junius L Jan 22 '17 at 19:44
  • @RifkanRazak, I hope you'll understand that it's routine to close these types of questions as duplicates - though you've mentioned that you've seen the marked question and it didn't help. Can you clarify *why* that is? I suspect your question isn't "how do I access an undefined variable?" but rather "Why isn't `$_POST['whatever']` set, and how do I debug it?". That is, the crux of this question isn't the literal accessing of an undefined variable, but rather why `$_POST` isn't what you think it is. Is that correct? – HPierce Jan 22 '17 at 20:15

1 Answers1

-1

Rifkan, you will need to outline why these errors are not self-explanatory and why the suggested duplicate question is not helpful. These errors mean what they sound like in ordinary English:

  • "Undefined variable" means that you are using a variable that is undefined. It is harder to be more explicit than that, other than to say that an uninitialised variable will not have the value that you expect (it does not exist).
  • "Undefined index" means you are using an array entry that similarly has not been initialised yet.

In both cases the cause is that you need to initialise the variable or array entry before using it.

Start with the first one, $email on line 7, and work out the circumstances in which $email would not be set. That will happen if:

  1. You visit the page outside of a form "post" operation
  2. A form is posted to this page that does not contain an "email" form element

If the purpose of this whole page is to create a user, then it would be a good idea to wrap the whole of your logic to test for the "post" method, since you don't want any of this logic to be run if the user is not attempting to register.

Note that you also have some security errors to consider:

  • A whacking great SQL injection vulnerability;
  • Passwords stored in plain text, which exposes users to secondary password reuse attacks;
  • No checking to see if the user typed the password in correctly.

Added as wiki because the question really is a duplicate as indicated, just adding something to help walk the OP through the problem.

Community
  • 1
  • 1
halfer
  • 19,824
  • 17
  • 99
  • 186