2010-07-25 41 views
0

有人可以告诉我为什么没有插入记录?这是我的一个页面: -mysql php crud问题

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title></title> 
    </head> 
    <body> 


     <form action="Adduser.php" method="post"> 

     <center> 
     <table> 
      <tr> 
       <td> 
        Username : 
       </td> 
       <td> 
        <input type="text" id="username" /> 
       </td> 
      </tr> 

      <tr> 
       <td> 
        Password : 
       </td> 
       <td> 
        <input type="text" id="password"/> 
       </td> 
      </tr> 

      <tr> 
       <td> 
        Email Address : 
       </td> 
       <td> 
        <input type="text" id="emailaddress"/> 
       </td> 
      </tr> 

      <tr> 
       <td> 
        Address : 
       </td> 
       <td> 
        <input type="text" id="Address"/> 
       </td> 
      </tr> 
      <tr> 
       <td colspan="2"> 
        <input type="Submit" name="submit" value="Add"/> 
       </td> 
      </tr> 

     </table> 
     </center> 
     <?php 
        mysql_connect('localhost', 'root', ''); 
      mysql_select_db('user'); 
      $query = mysql_query("Select * from tbluser"); 
      echo "<center>"; 
      echo '<table style="border:solid 2px black;">'; 
      while(($row = mysql_fetch_array($query)) != NULL) { 
       echo '<tr>'; 
       echo '<td>' . $row['UserName'] . '</td>'; 
       echo '<td>' . $row['Password'] . '</td>'; 
       echo '<td>' . $row['EmailAddress'] . '</td>'; 
       echo '<td>' . $row['Address'] . '</td>'; 
       echo '</tr>'; 
      } 
      echo '</table>'; 
      echo "</center>"; 
     ?> 
      </form> 
    </body> 
</html> 

问题就在这里(这是Adduser.php): -

的,如果条件永远不会被执行。有人可以告诉我为什么它不起作用?该$_POST数组包含任何通过var_dump

+2

对于初学者来说,你的文字输入都需要有'name = “... ”','不是ID =“ ...”' – 2010-07-25 16:06:26

回答

3

你必须使用name属性为每个输入字段,如

<input type="text" id="username" name="username" /> 

感谢

+0

可能要同时检查'如果($ _ SERVER [ 'REQUEST_METHOD' ] =='POST'){'而不是'if($ _POST ['submit'])''。您可能在某些时候忘记在您的提交按钮上设置一个名称,但REQUEST_METHOD将始终存在。 – 2010-07-25 19:11:15

1

这是Adduser.php网页上的代码打印时?如果它是不同的页面,当用户点击提交按钮时,Adduser.php页面将接收到http请求。

不是你需要在输入上使用name属性。所以当提交上面这个表单时,它会收到一个包含$ _POST ['submit'],$ _POST ['username'],$ _POST ['password']的http请求。这就是为什么在表单顶部,我首先通过使用PHP :: isset()函数检查提交按钮是否很漂亮。

IE。

<?php 
if(isset($_POST['submit'])){ 
//process post submission 
//perform mysql insert and notify user the result status 
}else{ 
//initialize form values 
$_POST['userName'] = ''; 
$_POST['password'] = ''; 
} 
?> 
<html> 
<body> 
<form action="<?=$_SERVER['PHP_SELF']?>"> 
<input type="text" name="username" value="<?=$_POST['userName']?>" /> 
<input type="text" name="password" value="<?=$_POST['password']?>" /> 
<input type="submit" name="submit" value="Add user"/> 
</form> 
</body> 
</html>