-1

I am having problem in this following code.It says undefined index on line 83.The second problem is that there is a huge gap between the text fill the required form and the input textbox of the form during output.Please help me out.The code is posted below.

<html>
    <head>
        <style>
            .error {color: #FF0000;}
        </style>
    </head>
    <body>
        <?php
            $firstnameErr = $lastnameErr = $emailErr = "";
            $firstname = $lastname = $email = "";

            if ($_SERVER["REQUEST_METHOD"] == "POST")
            {
                if (empty($_POST["firstname"]))
                {
                    $firstnameErr = "Name is required";
                }
                else
                {
                    $firstname = test_input($_POST["firstname"]);
                }

                if (empty($_POST["lastname"]))
                {
                    $lastnameErr = "Name is required";
                }
                else
                {
                    $lastname = test_input($_POST["lastname"]);
                }
                if (empty($_POST["email"]))
                {
                    $emailErr = "Email is required";
                }
                else
                {
                    $email = test_input($_POST["email"]);
                }
            }

            function test_input($data)
            {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }         
        ?>
        <div text align =center><h1>Eventous Info</h1></div>
        <h3>Fill the Required Form:</h3>
        <p><span class="error">*required field</span></p>
        <table>
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
                <tr><?php// echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
                    <td>Firstname:</td>
                    <td><input type="text" name="firstname" ></td>
                    <td><span class="error">* <?php echo $firstnameErr;?></span></td><br><br>
                </tr>
                <tr>
                    <td>Lastname:</td>
                    <td><input type="text" name="lastname" ></td>
                    <td><span class="error">* <?php echo $lastnameErr;?></span></td><br><br>
                </tr>
                <tr>
                    <td>Email</td>
                    <td><input type="text" name="email"></td>
                    <td><span class="error">* <?php echo $emailErr;?></span></td><br><br>
                </tr>
                <tr>
                    <td>Phone:</td>
                    <td><input type="text" name="number"><td><br><br>
                </tr>
            </table>
            <input type="submit" >
        </form>

        <?php
            $con = mysql_connect("localhost","ashu123","bangalore");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
            mysql_select_db("evantus", $con);

            $sql="INSERT INTO employee (firstname, lastname, email, phone )
              ***LINE-83***
            VALUES
            ('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[number]')";

            $sql = "select * from employee";
            $query = mysql_query( $sql );


            echo "<table>";
            echo "<tr><th>firstname</th>";

            echo "<th>lastname</th>";

            echo "<th>email</th>";

            echo "<th>phone</th></tr>";
            while( $row = mysql_fetch_assoc($query) )
            {
                echo "<tr><td>$row[firstname]</td>";
                echo "<td>$row[lastname]</td>";
                echo "<td>$row[email]</td>";
                echo "<td>$row[phone]</td></tr>";
            }

            echo "</table>";

            if (!mysql_query($sql,$con))
            {
                die('Error: ' . mysql_error());
            }
            mysql_close($con)
        ?>
  </body>
</html>
Colin R
  • 17,711
  • 2
  • 20
  • 28
user2894736
  • 1
  • 1
  • 3

2 Answers2

0

Your form has invalid html code. In short, just use:

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

    <table>
    <tr>
        <td>Firstname:</td>
        <td><input type="text" name="firstname" ></td>
        <td><span class="error">* <?php echo $firstnameErr;?></span><br /><br /></td>
    </tr>
    <tr>
        <td>Lastname:</td>
        <td><input type="text" name="lastname" ></td>
        <td><span class="error">* <?php echo $lastnameErr;?></span><br /><br /></td>
    </tr>
    <tr>
        <td>Email</td>
        <td><input type="text" name="email"></td>
        <td><span class="error">* <?php echo $emailErr;?></span><br /><br /></td>
    </tr>
    <tr>
        <td>Phone:</td>
        <td><input type="text" name="number"><br /><br /><td>
        <td></td>
    </tr>
    </table>

    <input type="submit" >

</form>

More about it: 1. After opening the <table> element, next one must be table row, so <form> must be a wrapper to your table. 2. You have placed breaks after closing the </td> tag, which is wrong - they should be inside table cell. 3. Ref: undefined index, guess it is only a warning that you are using the $_POST variable that does not exist.

skobaljic
  • 9,379
  • 1
  • 25
  • 51
0

I feel your insert query is not right, try the query below.

$sql="INSERT INTO employee (firstname, lastname, email, phone ) VALUES ('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['email']."','".$_POST['number']."')";
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56