2013-10-21 97 views
-1

我在這個代碼中有問題,它說83行上的未定義索引。第二個問題是文本之間填充所需格式和輸出中輸入的文本框。請幫助我。代碼發佈在下面。未定義的索引:姓氏,姓氏,電子郵件,line83

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

brb。數到行83. – christopher

+0

笏exct你想要做什麼? ur表單包含2個查詢(INSERT n SELECT)和u r只生成1個結果.. – Ashish

+0

我想插入數據到數據庫並顯示數據庫在我的php – user2894736

回答

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:未定義的索引,猜測它只是一個警告,表示您正在使用不存在的$_POST變量。

0

我覺得你的插入查詢不正確,請嘗試下面的查詢。

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