-4

Here is the php connectivity code:

<?php 

  $con=mysql_connect("127.0.0.1", "root", ""); 
  mysql_select_db("society");
  if(!$con)
  {
    echo "Failed to Connect to MySql".mysqli_connect_error();
  }
  else
  {
    echo"success";
  } 

  $query="INSERT INTO users (fname, lname, email, password)
                      VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')"; 
  mysql_query($query,$con);

?> 

This is the error. Tried, but I am not able to solve the error:

Notice: Undefined index: fname in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13

Notice: Undefined index: lname in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13

Notice: Undefined index: email in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13

Notice: Undefined index: password in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13

Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
Ninja Coder
  • 29
  • 1
  • 1
  • 4

2 Answers2

0

Try this

<?php 

  $con=mysql_connect("127.0.0.1", "root", ""); 
  mysql_select_db("society");
  if(!$con)
  {
  echo "Failed to Connect to MySql".mysql_connect_error();
  }
 else
 {
 echo"success";
 } 
 if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email'])) { 
 $query="INSERT INTO users (fname, lname, email, password)
                    VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')"; 
    mysql_query($query,$con);
    }

?>

Tal
  • 1,091
  • 12
  • 25
  • The mysql_* extension is deprecated. Missing sql injection prevention. No repsonse if the parameters are missing (i.e. failing silently). – VolkerK Sep 03 '15 at 09:06
0

Check if there is values for the POST your using in the query. If it is returning undefined_index is because the variables of the post that you are using isn't set. They don't even exist at the moment.

For instance:

If you debug $_POST['fname'] it will return false or empty. Try it.

Resolution:

Right now I think you're running this code without validating if the form has been submited or not. So, to start you should do this:

if(isset($_POST) && count($_POST) > 0){     

$con=mysql_connect("127.0.0.1", "root", ""); 
        mysql_select_db("society");
        if(!$con)
        {
          echo "Failed to Connect to MySql".mysqli_connect_error();
        }
        else
        {
          echo"success";
        } 

     $query="INSERT INTO users (fname, lname, email, password) VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')"; 
    mysql_query($query,$con);
}
Wilson -
  • 145
  • 8
  • @tal careful with your solution. Because your solution is forcing to variables being set when they're not mandatory. Imagine you dont want to fill the field email? You'll never insert data in the database... Careful. If you validate the form itself this way you can validate that the form has been submited. – Wilson - Sep 03 '15 at 09:00
  • The mysql_* extension is deprecated. Missing sql injection prevention. No repsonse if the parameters are missing (i.e. failing silently). – VolkerK Sep 03 '15 at 09:07
  • That isn't the problem here. The problem is the PHP error and the origin is what i've said. The way he's doing is wrong but isn't causing any bugs.. – Wilson - Sep 03 '15 at 09:13
  • It is a general problem and I promise you will get this kind of comment on a regular basis on SO if the answer doesn't reflect these topics. There are already enough sites on the web showing this type of broken code - try to make stackoverflow a better place ;-) – VolkerK Sep 03 '15 at 09:16
  • Yea, but this problem aint related to that. Im 200% sure :) – Wilson - Sep 03 '15 at 09:41
  • But I'm with you mate. I agree with you, but the bug here reported is different. And it seems he's not even receiving the Deprecated Notice, which means he is using PHP version inferior to 5.5. I agree with you, but the bug in this case is different. – Wilson - Sep 03 '15 at 09:53
  • And that's the reason why the question has been downvoted (so hopefully nobody else will find it by "blind" google search) and is now marked as a duplicate. We already failed the original mission goal of stackoverflow (encyclopedia-like, not a one-on-one tutorial/discussion) big time. And I share the guilt :) – VolkerK Sep 03 '15 at 09:59