2013-07-14 45 views
0

我创建一个表,每行有一个按钮删除,应该删除该行。但是,现在,当我单击删除时,“没有任何反应”,我必须刷新页面才能看到结果。请帮忙。php删除mySQL的行从<table>

我的代码的index.php

<body> 

    <?php include "include/connect.php"; ?> 

    <table> 
     <tr> 
      <th>ID</th> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Proffesion</th> 
      <th>Rank</th> 
      <th>Options</th> 
      <th>&nbsp;</th> 
     </tr> 
     <tr> 
      <form action="add.php" method="post"> 
       <td><input type="text" name="id" disabled size="5"></td> 
       <td><input type="text" name="fname"></td> 
       <td><input type="text" name="lname"></td> 
       <td><input type="text" name="prof"></td> 
       <td><input type="text" name="rank" size="5"></td> 
       <td><input type="submit" value="add" name="watta"></td> 
       <td>&nbsp;</td> 
      </form> 
     </tr> 

     <?php 
      $query = "SELECT * FROM staff"; 
      $vysledek = mysqli_query($link, $query); 

      while ($udaj = mysqli_fetch_array($vysledek)): 

       if($udaj[4]==0){ 
        echo "<tr class='zero'>"; 
       } else { 
        echo "<tr>";  
       } 



       echo "<td>" . $udaj[0] . "</td>"; 
       echo "<td>" . $udaj[1] . "</td>"; 
       echo "<td>" . $udaj[2] . "</td>"; 
       echo "<td>" . $udaj[3] . "</td>"; 
       echo "<td>" . $udaj[4] . "</td>"; 
       echo "<td class='btn'><a href=''>"; 

       ?> 
       <form action="delete.php" method="get"> 
        <input name="id" type="hidden" value="<?php echo $udaj[0]; ?>"> 
        <input name="what" type="submit" value="DELETE"> 
       </form> 

       <?php 
       echo "</a></td>"; 
       echo "<td class='btn'><a href=''>edit</a></td>"; 

       echo "</tr>"; 

      endwhile; 


     ?> 

    </table> 

</body> 

delete.php

<?php 

include "include/connect.php"; 

$toId = ($_GET["id"]); 

$queryy = "DELETE FROM staff WHERE id=$toId"; 
$vysledek = mysqli_query($link, $queryy); 

header('Location: http://www.w3dominik.com/x/phptest/'); 

connect.php

<?php 

include "config.php"; 

$link = mysqli_connect(SQL_HOST, SQL_USERNAME, SQL_PASSWORD, SQL_DBNAME); 
mysqli_set_charset($link, "utf8"); 

if (mysqli_connect_errno()) { 
printf("Connect failed: %s\n", mysqli_connect_error()); 
exit(); 
} 

现场演示在http://www.w3dominik.com/x/phptest/

+0

你连接到delete.php文件中的数据库? – 2013-07-14 19:53:59

+1

如果您在两个选项卡/窗口中打开页面并删除第一个选项卡中的一行并刷新该选项卡,那么当您在第二个选项中按删除(ON THAT ROW)时它将起作用。换句话说,我们需要看到更多的代码......你能拷贝所有的'index.php'和'delete.php'吗? (可能'connect.php'去掉你的详细信息等) – Steven

+0

你应该使用AJAX来实现这个[见] {http://openenergymonitor.org/emon/node/107} –

回答

0

看connect.php和让苏没有输出。如果在它之前有任何空格或空行输出,则header()将不起作用。很容易有错误地输入某种空白字符。 http://php.net/manual/en/function.header.php

+0

好的,现在,我已经添加了整个index.php和connect.php。现在呢? – user1505027

+0

正如文档所说:“请记住,必须在发送任何实际输出之前调用header(),或者使用普通HTML标记,文件中的空行或PHP进行发送。使用include或require函数或其他文件访问函数读取代码,并在调用header()之前输出空格或空行是非常常见的错误。文件中有许多空行,但很难判断它们是否导致问题。我会尽可能地内联(不包括'include')来查看是否可以解决问题 – edtheprogrammerguy

+0

这是错误的。表单提交到另一个页面,然后被重定向回来。如果'header'重定向不起作用,那么它会抛出错误并指出'头文件已经被发送了',并且不会重定向回表单页面。 – Steven