4 Replies - 1687 Views - Last Post: 17 March 2013 - 07:20 PM Rate Topic: -----

#1 poldz123  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 26-October 12

Notice: Undefined index

Posted 17 March 2013 - 06:28 PM

Why is it I always got undefine index and there is no data submitted to my database?

<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$regfirstname = $_POST['firstname'];
$reglastname = $_POST['lastname'];
$regpassword = $_POST['password'];
$regusername = $_POST['username'];
$regage = $_POST['age'];

$checkusername = mysql_query("SELECT * FROM members WHERE username = '$regusername' AND password = '$regpassword'");
if(mysql_num_rows($checkusername) != 0){
	die("username already taken");
}
if(mysql_num_rows($checkpassword) !=0){
	die("password already taken");
}

mysql_query("INSERT INTO members(username,password,firstname,lastname,age) VALUES ('$regusername','$regpassword','$regfirstname','$reglastname','$regage')")
or die(mysql_error());

echo "Thank you for registration";

?>


<FORM action="register.php" method="post">

    <TABLE BORDER="1">

      <TR>

    <TD><LABEL for="firstname">First name: </LABEL></TD>

              <TD><INPUT type="text" id="firstname"></TD>

              </TR>
    <TR><TD><LABEL for="lastname">Last name: </LABEL></TD>

              <TD><INPUT type="text" id="lastname"></TD></TR>

   <TR> <TD><LABEL for="username">Username: </LABEL></TD>

              <TD><INPUT type="text" id="username"></TD></TR>

    <TR><TD><LABEL for="password">Password: </LABEL></TD>

              <TD><INPUT type="password" id="password"></TD></TR>

                 <TR> <TD><LABEL for="Age">Age: </LABEL></TD>

            <TD><INPUT type="text" id="age"></TD></TR>

      </table>
      <P>


    <INPUT type="submit" value="Send"> <INPUT type="reset">

    </P>

 </FORM>


Is This A Good Question/Topic? 0
  • +

Replies To: Notice: Undefined index

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,362
  • Joined: 27-December 08

Re: Notice: Undefined index

Posted 17 March 2013 - 06:29 PM

Please post your error exactly as it appears.
Was This Post Helpful? 0
  • +
  • -

#3 poldz123  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 26-October 12

Re: Notice: Undefined index

Posted 17 March 2013 - 06:34 PM

Notice: Undefined index: firstname in C:\xampp\htdocs\register.php on line 12

Notice: Undefined index: lastname in C:\xampp\htdocs\register.php on line 13

Notice: Undefined index: password in C:\xampp\htdocs\register.php on line 14

Notice: Undefined index: username in C:\xampp\htdocs\register.php on line 15

Notice: Undefined index: age in C:\xampp\htdocs\register.php on line 16
Thank you for registration
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,362
  • Joined: 27-December 08

Re: Notice: Undefined index

Posted 17 March 2013 - 06:36 PM

The name attribute should be used instead of the id attribute to label inputs that are to be submitted. That should fix your problem.
Was This Post Helpful? 1
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Notice: Undefined index

Posted 17 March 2013 - 07:20 PM

You are making three major errors in your PHP code, and a couple of minor ones as well.

Here are the major problems:

  • You are assuming that the POST data was sent. As you are experiencing now, that is not a given. You ALWAYS need to make sure that the user input you are expecting is actually there before you try to use it. For that, use the isset or empty functions. Like:
    <?php
    if (!empty($_POST["name"]) && !empty($_POST["message"])) {
        // Do stuff with the user input.
    }
    else {
        // User input is missing. Show an error or something.
    }
    
    


  • Your code is wide open to SQL Injection. Nobody should be allowed near a SQL query in a PHP script before reading the article I just linked to. If you haven't read it already, I suggest you do it before typing so much as a single letter more!

  • You are assuming that your INSERT query did something, without actually making sure that it did. Use the mysql_affected_rows function to check whether or not something was inserted before telling the user it was. (It's generally a bad idea to lie to your users...)


And a few of the minor problems as well:

  • The mysql_query family of functions is outdated and should not be used. Instead use PDO or MySQLi. - Both of those support Prepared Statements, which provide a much much higher degree of security than anything the old MySQL API had to offer.

  • You should not quote variables unless they are supposed to be a part of a string. That is:
    // This is completely pointless:
    doStuff("$param1", "$param2");
    
    // It should be:
    doStuff($param1, $param2);
    
    


  • Read up on proper error handling. It's far better to set up error reporting properly and use trigger_error than it is to use die to print errors to the screen.
    // Lazy!
    mysql_query($sql) or die(mysql_error());
    
    // Better
    mysql_query($sql) or trigger_error(mysql_error(), E_USER_ERROR);
    
    // Even better
    $result = mysql_query($sql);
    if (!result) {
        echo "An error has occurred! Please try again, or something.";
        trigger_error(mysql_error(), E_USER_ERROR)
    }
    
    


  • If you need to count the number of rows matching a certain criteria in your database, use the SQL COUNT() function. Don't tell the database to return a bunch of data so you can count it yourself, when the database is more than capable of doing so for you.
    // Bad
    $result = mysql_query("SELECT * FROM users WHERE username = '{$username}'");
    if ($result) {
        if (mysql_num_rows($result) == 1) {
            // Do stuff if user exists
        }
    }
    
    // Good
    $result = mysql_query("SELECT COUNT(*) FROM users WHERE username = '{$username}'");
    if ($result) {
        $count = (int)mysql_result($result, 0, 0);
        if ($count == 1) {
            // Do stuff if user exists
        }
    }
    
    

This post has been edited by Dormilich: 17 March 2013 - 11:18 PM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1