-1

我在以下代码中遇到问题。它在第 83 行显示未定义的索引。第二个问题是在输出期间填充所需表单的文本与表单的输入文本框之间存在巨大差距。请帮帮我。代码贴在下面。

<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>
4

2 回答 2

0

您的表单包含无效的 html 代码。简而言之,只需使用:

<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>

更多信息: 1. 打开<table>元素后,下一个元素必须是表格行,因此<form>必须是表格的包装器。2.您在关闭</td>标签后放置了中断,这是错误的 - 它们应该在表格单元格内。3. Ref: undefined index,猜猜这只是一个警告,你正在使用$_POST不存在的变量。

于 2013-10-21T10:53:59.777 回答
0

我觉得你的插入查询不对,试试下面的查询。

$sql="INSERT INTO employee (firstname, lastname, email, phone ) VALUES ('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['email']."','".$_POST['number']."')";
于 2013-10-21T10:58:38.923 回答